如何在Hibernate注释应用程序中外部化命名查询? [英] How do I externalize named queries in a Hibernate annotations app?
问题描述
@NamedQueries
和 @NamedQuery
正在伤害。 有没有一种方法可以将多个文件外化?
您可以将查询放入 package-info.java
类中,比如说,您的域对象的根包中。但是,您必须使用Hibernate自己的 @NamedQueries
和 @NamedQuery
注释,而不是来自 javax.persistence
。
示例 package-info.java
file:
@ org.hibernate.annotations.NamedQueries({
@ org.hibernate.annotations.NamedQuery (
name =foo.findAllUsers,
query =from Users)
})
package com.foo.domain;
然后,您必须将该包添加到 AnnotationConfiguration
。我使用Spring,所以在这里设置 annonatedPackages
属性:
< bean id =sessionFactory
class =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =annotatedClasses>
< list>
...
< / list>
< / property>
< property name =annotatedPackages>
< list>
<值> com.foo.domain< /值>
< / list>
< / property>
您也可以将type和过滤相同文件中的定义。
Is there a way to externalize HQL named queries to an external file. I have too many named queries and using @NamedQueries
and @NamedQuery
at the head of my entities classes is hurting.
Is there a way to externalize to several files?
You can put the queries into package-info.java
class, in, say, root package of your domain objects. However, you must use Hibernate's own @NamedQueries
and @NamedQuery
annotations, rather than those from javax.persistence
.
Example package-info.java
file:
@org.hibernate.annotations.NamedQueries({
@org.hibernate.annotations.NamedQuery(
name = "foo.findAllUsers",
query="from Users")
})
package com.foo.domain;
Then, you have to add the package to your AnnotationConfiguration
. I use Spring, so there it's a matter of setting annonatedPackages
property:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
...
</list>
</property>
<property name="annotatedPackages">
<list>
<value>com.foo.domain</value>
</list>
</property>
You can also put type and filter definitions in the same file as well.
这篇关于如何在Hibernate注释应用程序中外部化命名查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!