根据Hibernate文档的这一部分,我应该能够在HQL中查询任何java类
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-polymorphism
不幸的是,当我运行此查询...
"from Transaction trans where trans.envelopeId=:envelopeId"
我收到消息“事务未映射[来自Transaction trans where trans.envelopeId =:envelopeId]”。
Transaction是一个接口,我必须实现它的实体类,我希望在HQL查询中返回一个Transaction类型的Collection。
答案 0 :(得分:11)
确实,根据Polymorphic queries上的Hibernate文档:
Hibernate查询可以命名任何Java from子句中的类或接口。 查询将返回所有实例 扩展它的持久化类 类或实现接口。该 以下查询将返回全部 持久对象:
from java.lang.Object o
Named接口可能是 由各种持久性实施 类:
from Named n, Named m where n.name = m.name
但由于接口未映射(因而未知),因此需要在HQL查询中使用完全限定名称:
from qualified.name.Transaction trans where trans.envelopeId=:envelopeId
此将返回实现Transaction
接口的所有持久化类的实例。
答案 1 :(得分:1)
尝试导入接口,这样您就不必指定完整路径。我使用一个名为imports.hbm.xml的文件来处理所有接口:
<hibernate-mapping package="com...path.to.implementations">
<import class="com.path.to.interfaces.Transaction" rename="Transaction"/>
...
</hibernate-mapping>
然后将其添加到配置中,就像普通的映射文件一样。
答案 2 :(得分:0)
我认为您需要将接口映射为父类,将实现类映射为其子类。
http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#inheritance-strategies