这是场景:
我有 2 个用 hibernate 映射的类:“Foo”和“EnhancedFoo”
两个类都映射到同一个表“foo”
EnhancedFoo 扩展了 Foo
Foo.hbm.xml 包含一个命名查询“find active foos”,如下所示:
from Foo foo where foo.active = true
现在,如果我尝试加载配置,我会得到这个异常:
could not resolve property: active of: EnhancedFoo [
from EnhancedFoo foo where foo.active = true
]
这是正确的,因为在EnhancedFoo.hbm.xml 中没有映射属性“active”,但是为什么hibernate 用“EnhancedFoo”替换“Foo”?
这是我尝试过的:
尝试在映射文件上添加“实体名称”属性,如下所示:
<class name="myproject.data.entity.Foo" table="foo" entity-name="Foo">
.......
<class name="myproject.data.entity.EnhancedFoo" table="foo" entity-name="EnhancedFoo">
然后配置加载正常,但每当我尝试插入 Foo 时,我都会得到这个:
org.hibernate.MappingException: Unknown entity: myproject.data.entity.Foo
这些是代码的相关部分:
//Foo.java
public class Foo
{
private long id;
private boolean active;
// getters and setters
}
//EnhancedFoo.java
public class EnhancedFoo extends Foo
{
private String extraProperty
// getter and setter
}
//Foo.hbm.xml
//.....
<class name="myproject.data.entity.Foo" table="foo" entity-name="Foo">
<id column="id" name="id">
<generator class="assigned"/>
</id>
<property name="active" column="active" />
</class>
<query name="find_active_foos">
<![CDATA[
from Foo foo where foo.active = true
]]>
</query>
//EnhancedFoo.hbm.xml
//.....
<class name="myproject.data.entity.EnhancedFoo" table="foo" entity-name="EnhancedFoo">
<id column="id" name="id">
<generator class="assigned"/>
</id>
</class>