2014-01-14 69 views
7

我有抽象类:查询按类型在Spring数据JPA

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

和几个扩展类,如:

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

我也有第三个实体:

@Entity 
public class C { 

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

问题是,我如何构建C实体库中的Spring Data JPA finder,只查询扩展了A的对象和期望的类型?

+0

添加只读属性你的鉴别鉴别值的名称,或者尝试从'样本S选择S其中TYPE(S)=:type' – MariuszS

+0

[Java/JPA |的可能重复查询与指定的继承类型](http://stackoverflow.com/questions/4884249/java-jpa-query-with-specified-inherited-type) – MariuszS

+0

第一件事我问春天的数据和第二个需要查询不是键入(c)但键入(c.objectA) –

回答

7

您可以使用您所定义的“类型”

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