Hibernate HQL与接口 [英] Hibernate HQL with interfaces

查看:118
本文介绍了Hibernate HQL与接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Hibernate文档的这一部分,我应该能够查询HQL中的任何Java类



http://docs.jboss.org/hibernate/core/3.3/reference/en/html /queryhql.html#queryhql-polymorphism



不幸的是,当我运行这个查询时......



<$从交易trans其中trans.envelopeId =:envelopeId



>我得到消息事务没有被映射[来自事务trans其中trans.envelopeId =:envelopeId]。

事务是一个接口,我必须实体类实现它,我希望在HQL查询中返回一个Collection类型的Transaction。

解决方案

的确,根据Hibernate文档< a href =http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-polymorph hibernate查询可以命名任何Java
类或接口在from子句中。
查询将返回所有
持久化类的实例,这些类扩展了
类或实现了接口。
以下查询将返回所有
持久对象:

 来自java.lang.Object o 

接口Named可能是
,由各种持久
类实现:

  from Named n,Named m where n.name = m.name 


但是因为接口没有被映射(因此未知),所以您需要在HQL查询中使用完全限定名称:

 来自qualified.name.Transaction trans其中trans.envelopeId =:envelopeId 
$ b

这个返回实现您的 Transaction 接口的所有持久化类的实例。


According to this section of the Hibernate documentation I should be able to query any java class in HQL

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-polymorphism

Unfortunately when I run this query...

"from Transaction trans where trans.envelopeId=:envelopeId"

I get the message "Transaction is not mapped [from Transaction trans where trans.envelopeId=:envelopeId]".

Transaction is an interface, I have to entity classes that implement it, I want on HQL query to return a Collection of type Transaction.

解决方案

Indeed, according to the Hibernate documentation on Polymorphic queries:

Hibernate queries can name any Java class or interface in the from clause. The query will return instances of all persistent classes that extend that class or implement the interface. The following query would return all persistent objects:

from java.lang.Object o

The interface Named might be implemented by various persistent classes:

from Named n, Named m where n.name = m.name

But because the interface is not mapped (and thus unknown), you need to use the fully qualified name in your HQL query:

from qualified.name.Transaction trans where trans.envelopeId=:envelopeId

This will return instances of all persistent classes that implement your Transaction interface.

这篇关于Hibernate HQL与接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆