Hibernate HQL无法读取作为子类实例的属性

Hibernate HQL无法读取作为子类实例的属性,hibernate,inheritance,polymorphism,hql,subclass,Hibernate,Inheritance,Polymorphism,Hql,Subclass,首先,我想说的是,我已经阅读了几十篇关于hibernate的继承映射或多态获取的文章,但没有找到问题的解决方案。 虽然情况很简单 执行以下代码时: List=sessionFactory.getCurrentSession() .createQuery(“从m中选择m”).list(); 我得到以下错误: org.hibernate.ErrorClassException:id为1的对象不属于指定的子类:com.myPack.Fruit(鉴别器:Orange) 实际上,我看不懂抽象类fruit,

首先,我想说的是,我已经阅读了几十篇关于hibernate的继承映射或多态获取的文章,但没有找到问题的解决方案。 虽然情况很简单

执行以下代码时: List=sessionFactory.getCurrentSession() .createQuery(“从m中选择m”).list(); 我得到以下错误: org.hibernate.ErrorClassException:id为1的对象不属于指定的子类:com.myPack.Fruit(鉴别器:Orange)

实际上,我看不懂抽象类fruit,它的子类是Orange或Apple

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "fruit")
public abstract class Fruit {

    @Id
    @GeneratedValue
    @Column(name = "id", unique = true)
    private Integer id;
}

@Entity(name = "Orange") //this is the discriminatorValue stored in dtype column
public class Orange extends Fruit {
}

@Entity(name = "Apple") //this is the discriminatorValue stored in dtype column
public class Orange extends Fruit {
}

@Entity
@Table(name = "meal")
public class Meal {
    ...

    @ManyToOne
    @JoinColumn(name = "fruit_id", nullable = false)
    private Fruit fruit  = null;
}
一顿饭含有水果,可以是橘子或苹果

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "fruit")
public abstract class Fruit {

    @Id
    @GeneratedValue
    @Column(name = "id", unique = true)
    private Integer id;
}

@Entity(name = "Orange") //this is the discriminatorValue stored in dtype column
public class Orange extends Fruit {
}

@Entity(name = "Apple") //this is the discriminatorValue stored in dtype column
public class Orange extends Fruit {
}

@Entity
@Table(name = "meal")
public class Meal {
    ...

    @ManyToOne
    @JoinColumn(name = "fruit_id", nullable = false)
    private Fruit fruit  = null;
}
获取不是惰性的,因此我不应该对代理有任何问题。 我不明白为什么hibernate无法找出水果表中的数据类型列中的鉴别器Orange或Apple所包含的水果是橙色还是苹果

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "fruit")
public abstract class Fruit {

    @Id
    @GeneratedValue
    @Column(name = "id", unique = true)
    private Integer id;
}

@Entity(name = "Orange") //this is the discriminatorValue stored in dtype column
public class Orange extends Fruit {
}

@Entity(name = "Apple") //this is the discriminatorValue stored in dtype column
public class Orange extends Fruit {
}

@Entity
@Table(name = "meal")
public class Meal {
    ...

    @ManyToOne
    @JoinColumn(name = "fruit_id", nullable = false)
    private Fruit fruit  = null;
}
如果能解决这个问题,我将不胜感激。 (对不起,我的英语含糊不清)

经过进一步的研究,我发现@DiscriminatorOptions(force=true)可以解决我的问题,但最终……无能为力。 我学到的是,即使hibernate拥有所有信息来查找具体的类,在某些情况下也需要@DiscriminatorOptions(force=true)之类的注释。 无论如何,我想告诉hibernate:“hibernate不要尝试实例化抽象水果类,因为它是抽象的。hibernate尝试依赖于@DiscriminatorValue设置为on的实体。”。。但是hibernate是聋子:(

替换这个,然后再试一次

List meals = sessionFactory.getCurrentSession().createQuery("from Meal").list();

我在使用Spring 4.0.2和Hibernate 4.3.4时遇到了完全相同的问题。我认为我的项目的依赖项中缺少了一些东西,或者可能只是Hibernate中的一个bug


我建议您回滚您的Hibernate版本,我拥有的上一个工作版本是4.2.4,我正在恢复到该版本。

将fom createQuery(“从餐中选择m”)更改为createQuery(“从餐中选择m”)在4.1.0.Final hibernate版本中不起作用。但移动到4.2.4或4.3.4版本时,行为不同,但在这两种情况下问题仍然存在:createQuery(“从膳食中”)意味着createQuery(“从膳食中选择m”)异常表示没有异常,但膳食的属性fruit为null。毕竟,这在功能上是相同的问题。将fom 4.1.0.Final hibernate版本更改为4.2.4或4.3.4版本时,行为不同,但在这两种情况下,问题仍然存在:在4.1.0.Final createQuery中(“从膳食m中选择m”)表示4.2.4或4.3.4 createQuery中的异常(“从膳食m中选择m”)并不意味着例外,但膳食的属性fruit为null。毕竟,这在功能上是相同的问题。