10

有没有办法将 HQL 命名查询外部化到外部文件。我有太多的命名查询,并且在我的实体类的头部使用@NamedQueries和伤害。@NamedQuery

有没有办法将多个文件外部化?

4

4 回答 4

10

您可以将查询放入package-info.java类中,例如,在域对象的根包中。但是,您必须使用 Hibernate 自己的@NamedQueries@NamedQuery注释,而不是来自javax.persistence.

示例package-info.java文件:

@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>
      <value>com.foo.domain</value>
  </list>
</property>

您也可以将类型和过滤器定义放在同一个文件中。

于 2009-03-03T15:43:58.353 回答
3

我认为这是不可能的,因为注释属性/属性值必须在编译时可用。因此,字符串不能外化为需要通过某种进程读取的文件。

我试图查找 package-info.java 是否可以提供某些东西,但找不到任何东西。

组织的另一种策略可能是将查询作为常量存储在类中。

在您的实体类中:

@NamedQuery(name="plane.getAll", query=NamedQueries.PLANE_GET_ALL)

然后为您的查询常量定义一个类:

public class NamedQueries {
    ...
    public static final String PLANE_GET_ALL = "select p from Plane p";
    ...
}
于 2009-03-02T22:34:08.703 回答
3

也许这不是作者所要求的(外部化到非 java 文件),但这就是我解决它的方法:

1.) 在我添加mappingResources到 sessionFactory的应用程序上下文 xml 文件中

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
      <list>
        <value>META-INF/Country.hbm.xml</value>
      </list>
    </property>
    <property name="annotatedClasses">
        <util:list>
            <value>com.example.Country</value>
        </util:list>
    </property>
    <property name="hibernateProperties" ref="hibernateProperties" />
</bean>

在那个 Country.hbm.xml 我有

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings 
    xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
    version="2.0">

    <entity class="com.example.Country">

        <named-query name="countryFindByCode">
            <query><![CDATA[
                select c
                  from Country c
                 where c.code = :code
            ]]></query>
        </named-query>

        <named-query name="countryFindByName">
            <query><![CDATA[
                select c
                  from Country c
                 where c.name = :name
            ]]></query>
        </named-query>

    </entity>

</entity-mappings>

我用它来定义命名查询,实体配置的其余部分在注释中。

也许这对某人有帮助。

于 2013-03-04T18:13:45.337 回答
-2
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
      <list>
        <value>META-INF/Country.hbm.xml</value>
      </list>
    </property>
    <property name="annotatedCla 
                  from Country c
                 where c.name = :name
            ]]></query>
        </named-query>

    </entity>

</entity-mappings>
于 2016-09-07T05:41:35.997 回答