Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query by type in Spring Data JPA

I've abstract class:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class A {
  ...
}

and few extending classes, like:

@Entity
public class B extends A {
  ...
}

I also have third entity:

@Entity
public class C  {

  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  private A objectA;
  ...
}

And the question is, how can I construct Spring Data JPA finder in C entity repository to query only objects extending A with desired type?

like image 319
Jakub Kubrynski Avatar asked Jan 14 '14 20:01

Jakub Kubrynski


Video Answer


1 Answers

You can use the name of discriminator value which you've defined "type"

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@Table(name = "abc")
public abstract class Abc {
....

-------------------------------------------

@Query("select a from Abc a where type = ?1")
List<Abc> findByType(String typeValue);
like image 74
Tsolak Barseghyan Avatar answered Oct 01 '22 20:10

Tsolak Barseghyan