java 如何在 Hibernate 注释应用程序中外部化命名查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/602397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I externalize named queries in a Hibernate annotations app?
提问by flybywire
Is there a way to externalize HQL named queries to an external file. I have too many named queries and using @NamedQueriesand @NamedQueryat the head of my entities classes is hurting.
有没有办法将 HQL 命名查询外部化到外部文件。我有太多的命名查询,@NamedQueries并且@NamedQuery在实体类的头部使用和是有害的。
Is there a way to externalize to several files?
有没有办法外部化到多个文件?
采纳答案by javashlook
You can put the queries into package-info.javaclass, in, say, root package of your domain objects. However, you must use Hibernate's own @NamedQueriesand @NamedQueryannotations, rather than those from javax.persistence.
您可以将查询放入package-info.java类中,例如,域对象的根包中。但是,您必须使用 Hibernate 自己的@NamedQueries和@NamedQuery注释,而不是来自javax.persistence.
Example package-info.javafile:
示例package-info.java文件:
@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 annonatedPackagesproperty:
然后,您必须将包添加到您的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>
You can also put type and filter definitions in the same file as well.
您也可以将类型和过滤器定义放在同一个文件中。
回答by Betlista
Maybe this is not exactly what author asked for (to externalize to non-java file), but this is how I solved it:
也许这不是作者所要求的(外部化为非 java 文件),但这就是我解决它的方法:
1.) in my application context xml file I added mappingResourcesto sessionFactory
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>
and in that Country.hbm.xml I have
在那个 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>
I used that just to define named queries, the rest of entity configuration is in annotations.
我用它来定义命名查询,实体配置的其余部分在注释中。
Maybe that helps someone.
也许这对某人有帮助。
回答by Matt Sidesinger
I don't think that this is possible as Annotation attribute/property values must be available at compile time. Therefore, Strings cannot be externalized to a file that needs to be read in by some sort of process.
我认为这是不可能的,因为注释属性/属性值必须在编译时可用。因此,字符串不能被外部化为需要通过某种进程读入的文件。
I tried to find if there was something that package-info.java might be able to provide, but could not find anything.
我试图找到 package-info.java 是否可以提供一些东西,但找不到任何东西。
An alternative strategy for organization could be storing the queries as constants in a Class.
组织的另一种策略可能是将查询存储为类中的常量。
In your entity class:
在您的实体类中:
@NamedQuery(name="plane.getAll", query=NamedQueries.PLANE_GET_ALL)
Then define a class for your query constants:
然后为您的查询常量定义一个类:
public class NamedQueries {
...
public static final String PLANE_GET_ALL = "select p from Plane p";
...
}
回答by vikas verma
<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>

