Reputation: 115
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.
Upvotes: 3
Views: 3882
Reputation: 357
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.
Upvotes: 0
Reputation: 815
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
Upvotes: 1