使用jpa和hibernate在orm.xml中定义命名查询 [英] define named query in orm.xml with jpa and hibernate
问题描述
我试图将我的命名查询放在我的orm.xml中(把persistence.xml放在META-INF中),但是我的orm.xml似乎被hibernate / jpa忽略了。
当我尝试使用em.createNamedQuery(myQuery)创建我的命名查询时,它返回它无法找到此查询。
我使用注释,我想将orm.xml中的命名查询外部化(仅此)。
这是我的持久性。 xml:
$ b
<?xml version =1.0encoding =UTF-8? >
< persistence xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsdversion =1.0>
< persistence-unit name =defaulttransaction-type =RESOURCE_LOCAL>
< mapping-file> META-INF / orm.xml< / mapping-file>
< class> com.mysite.Account< / class>
<属性>
< property name =hibernate.cache.provider_classvalue =net.sf.ehcache.hibernate.EhCacheProvider/>
< property name =hibernate.cache.use_query_cachevalue =true/>
< property name =hibernate.cache.use_second_level_cachevalue =true/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.format_sqlvalue =true/>
< property name =use_sql_commentsvalue =false/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MYSQLDialect/>
< property name =hibernate.c3p0.min_sizevalue =5/>
< property name =hibernate.c3p0.max_sizevalue =20/>
< property name =hibernate.c3p0.timeoutvalue =300/>
< property name =hibernate.c3p0.max_statementsvalue =50/>
< property name =hibernate.c3p0.idle_test_periodvalue =3000/>
< property name =hibernate.search.default.directory_providervalue =org.hibernate.search.store.FSDirectoryProvider/>
< / properties>
< / persistence-unit>
< /持久性>
这里是我的orm.xml
<?xml version =1.0encoding =UTF-8?>
< entity-mappings xmlns =http://java.sun.com/xml/ns/persistence/ormxmlns: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_1_0.xsdversion =1.0>
< package> com.mysite< / package>
< entity class =Account>
< sql-result-set-mapping name =nicknames>
< column-result name =nickname/>
< / sql-result-set-mapping>
< table name =Account/>
< named-native-query name =myQueryresult-set-mapping =nicknames>
< query><![CDATA [从帐户a选择一个.nickname]]>
< / query>
< / named-native-query>
< / entity>
< / entity-mappings>
我做错了什么?为什么我的orm.xml被忽略?
谢谢 好的我终于明白了!
我正在将META-INF目录中的orm.xml保存起来。当我把这个文件移动到我的包中有我的域对象的时候(比如在我的例子中:com.mysite),orm.xml不会被忽略,并且全部运行。
我还需要更改映射文件(persistence.xml)中的路径:com / mysite / orm.xml
I'm trying to put my named queries in my orm.xml (put in META-INF with persistence.xml) but my orm.xml seems to be ignored by hibernate/jpa.
When I try to create my named query with em.createNamedQuery("myQuery"), it returns that it can't find this query.
I use annotation and I would like to externalize my named queries in orm.xml (only that).
Here is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/orm.xml</mapping-file>
<class>com.mysite.Account</class>
<properties>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="use_sql_comments" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MYSQLDialect" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
<property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider" />
</properties>
</persistence-unit>
</persistence>
here is my orm.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_1_0.xsd" version="1.0">
<package>com.mysite</package>
<entity class="Account">
<sql-result-set-mapping name="nicknames">
<column-result name="nickname" />
</sql-result-set-mapping>
<table name="Account" />
<named-native-query name="myQuery" result-set-mapping="nicknames">
<query><![CDATA[select a.nickname from Account a]]>
</query>
</named-native-query>
</entity>
</entity-mappings>
What I'm doing wrong ? Why my orm.xml is ignored ?
thanks
Ok I finally got it !
I was saving my orm.xml in META-INF directory. When I move this file to my package where I have my domain object (like in my example: com.mysite), the orm.xml is not ignored and all run.
I also need to change the path in mapping-file (persistence.xml) : com/mysite/orm.xml
这篇关于使用jpa和hibernate在orm.xml中定义命名查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!