Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
269 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...