开发者

@Inheritance TABLE_PER_CLASS criteria strange behavior

开发者 https://www.devze.com 2023-03-08 12:37 出处:网络
In my application I have a mapping like this @MappedSuperclass public abstract class Base implements Serializable {

In my application I have a mapping like this

@MappedSuperclass
public abstract class Base implements Serializable {

@Entity
@Table(name = "LevelOne")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class LevelOne extends Base {

@Entity
@Table(name = "LevelTwo")
public class LevelTwo extends LevelOne {

@Entity
@Table(name = "LevelThree")
public class LevelThree extends LevelTwo {

The tables are created in the DB as expected. The pr开发者_运维百科oblem I have comes when I try to create a query like this:

session.getCurrentSession().createCriteria(LevelOne.class, "levelOne"). [..] .list();

I get results from all the other LevelX tables not only from the LevelOne Table.

I'm not sure if this behavior is expected or not or if my mapping has an error by not using the an abstract class with the "@Inheritance" annotation, however I would need to get only the "LevelOne" results. How I could get them?


Yes, it's an expected behaviour. If you need LevelOne only, add a restriction on the implicit class property:

session.getCurrentSession()
    .createCriteria(LevelOne.class, "levelOne")
    .add(Restrictions.eq("class", LevelOne.class))
    . [..] .list(); 
0

精彩评论

暂无评论...
验证码 换一张
取 消