Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-data : How to query a subtype using a specification

I have three entites like this:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE")
public abstract class A
{
    // fields, getters, setters
}

@Entity
@DiscriminatorValue("B")
public class B extends A
{
    // fields, getters, setters
}

@Entity
@DiscriminatorValue("C")
public class C extends A
{
    // fields, getters, setters
}

I also have a repository like this:

@Repository
public interface ADao extends JpaRepository<A, Long>, JpaSpecificationExecutor<A> 
{
}

On this repository, I have a method

List<A> findAll(Specification<A> s)

which is defined in JpaSpecificationExecutor.

My question is: how can I make a request using a specification on a field declared solely in B ?

Thanks in advance, Jerome

like image 799
jhek Avatar asked Dec 03 '13 12:12

jhek


1 Answers

create a

@Repository public interface BDao extends ADao<B>{}

and

public interface ADao extends JpaRepository<T extends A, Long>, JpaSpecificationExecutor<A> 

and then inject BDao to query B types

like image 68
Hakob Hakobyan Avatar answered Sep 19 '22 12:09

Hakob Hakobyan