我正在尝试将我的命名查询放入orm.xml(与persistence.xml一起放入META-INF中),但是hibernate / jpa似乎忽略了我的orm.xml。
当我尝试使用em.createNamedQuery(“ myQuery”)创建命名查询时,它返回找不到该查询的信息。
我使用注释,我想在orm.xml中外部化我的命名查询(仅此)。
这是我的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>
这是我的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>
我做错了什么?为什么我的orm.xml被忽略?
谢谢
好吧,我终于明白了!
我将orm.xml保存在META- INF目录中。当我将此文件移动到我的域对象所在的包中时(例如在我的示例中:com.mysite),orm.xml不会被忽略并且全部运行。
我还需要更改映射文件(persistence.xml)中的路径:com / mysite / orm.xml