简体   繁体   中英

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.

How to tell Hibernate that I really only want baseclass and not subclasses too.

you could use type operator. please look at the following link

How do I query for only superclass entities in a jpql query?

or( Java /JPA | Query with specified inherited type )

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:

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) , then your create a repository for the superclass and you fetch your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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