简体   繁体   English

Spring Jpa继承类型联接选择仅基类

[英]Spring Jpa inheritance type joined select only baseclass

I got problem with select only base class and cannot find solution for this. 我只选择基类时遇到问题,无法为此找到解决方案。 I always get subclasses too and not only base class. 我也总是获得子类,而不仅仅是基类。

Consider I have base class 考虑我有基础课

@Entity
@Inheritance(strategy = InheritanceType.JOINED)   
public class BaseClass {
  @Id
  @GeneratedValue
  private Long id;
  private Date date;
  // some fields
}

now I inherite 现在我继承了

@Entity  
public class SubClassA extends BaseClass {
  // some fields
}
interface BaseClassRep extends JpaRepository<BaseClass,Long>{
  @Query(nativeQuery = true, value = "select id,date from baseclass where date = ?1")
  public BaseClass getByDate(Date date)
}

Problem is that BaseClassRep is not only returns BaseClass but SubClassA as well and all other subclasses that inherits from BaseClass. 问题在于,BaseClassRep不仅返回BaseClass,而且还返回SubClassA以及从BaseClass继承的所有其他子类。

How to tell Hibernate that I really only want baseclass and not subclasses too. 如何告诉Hibernate我真的只想要基类而不想要子类。

you could use type operator. 您可以使用类型运算符。 please look at the following link 请看以下链接

How do I query for only superclass entities in a jpql query? 如何在jpql查询中仅查询超类实体?

or( Java /JPA | Query with specified inherited type ) 或( Java / JPA |具有指定继承类型的查询

select id,date from baseclass b where date = ?1 and b.class = ?2

I think you have to use the TABLE_PER_CLASS inheritance type: 我认为您必须使用TABLE_PER_CLASS继承类型:

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) , then your create a repository for the superclass and you fetch your data. @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) ,然后为超类创建一个存储库,然后获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM