如何在Hibernate注释应用程序中外部化命名查询? [英] How do I externalize named queries in a Hibernate annotations app?

查看:104
本文介绍了如何在Hibernate注释应用程序中外部化命名查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将HQL命名查询外部化为外部文件。我有太多的命名查询,并且在我的实体类的头部使用 @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屋!

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