8
votes

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?

1
Add read only property for your discriminator or try select s from Sample s where TYPE(s) = :type - MariuszS
First thing I asked about spring-data and second I I need to query not for type(c) but type (c.objectA) - Jakub Kubrynski

1 Answers

11
votes

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);