Java 使用 jpa 和 hibernate 在 orm.xml 中定义命名查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2373369/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:40:34  来源:igfitidea点击:

define named query in orm.xml with jpa and hibernate

javahibernateormjpanamed-query

提问by Jerome Cance

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.

我试图将我的命名查询放在我的 orm.xml 中(放在带有persistence.xml 的 META-INF 中),但我的 orm.xml 似乎被 hibernate/jpa 忽略了。

When I try to create my named query with em.createNamedQuery("myQuery"), it returns that it can't find this query.

当我尝试使用 em.createNamedQuery("myQuery") 创建命名查询时,它返回找不到此查询。

I use annotation and I would like to externalize my named queries in orm.xml (only that).

我使用注释,我想在 orm.xml 中外部化我的命名查询(仅此而已)。

Here is my persistence.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>

here is my orm.xml

这是我的 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 ?

我做错了什么?为什么我的 orm.xml 被忽略了?

thanks

谢谢

采纳答案by Jerome Cance

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.

我将 orm.xml 保存在 META-INF 目录中。当我将此文件移动到我拥有域对象的包中时(例如在我的示例中:com.mysite),orm.xml 不会被忽略并全部运行。

I also need to change the path in mapping-file (persistence.xml) : com/mysite/orm.xml

我还需要更改映射文件(persistence.xml)中的路径:com/mysite/orm.xml

回答by Arthur Ronald

As said

正如所说

When I try to create my named query with em.createNamedQuery("myQuery"), it returns that it can not find this query.

当我尝试使用 em.createNamedQuery("myQuery") 创建我的命名查询时,它返回它找不到此查询

You are right. But you forget the following

你是对的。但是你忘记了以下内容

If you place a named query definition inside a element, instead of the root, it is prefixed with the name of the entity class

如果将命名查询定义放在元素内,而不是根,则它以实体类的名称为前缀

So you need to call your namedQuery as

所以你需要将你的 namedQuery 称为

 em.createNamedQuery("Account.myQuery")

I am curious: Does your Account class is stored in the root classpath ??? If not, you have fix its missing package. Suppose Account class is stored inside br.com.hibernate.model.domain.Account. So you should declare your entity as

我很好奇:您的 Account 类是否存储在根类路径中???如果没有,你已经修复了它丢失的包。假设 Account 类存储在 br.com.hibernate.model.domain.Account 中。所以你应该将你的实体声明为

<entity class="br.com.hibernate.model.domain.Account" instead

And you need to call your namedQuery as

您需要将您的 namedQuery 称为

em.createNamedQuery("br.com.hibernate.model.domain.Account.myQuery") instead

Just an adivice: when you are using Hibernate as your Persistence Provider, you do not need to define your Entity class in persistence.xml file.

只是一个建议:当你使用 Hibernate 作为你的持久化提供者时,你不需要在 persistence.xml 文件中定义你的实体类。

regards,

问候,

回答by James Selvakumar

"META-INF/orm.xml" is the default mapping file that will be looked upon by any JPA compliant entity manager.

“META-INF/orm.xml”是任何符合 JPA 的实体管理器都会查看的默认映射文件。

In fact, if your mapping file is "META-INF/orm.xml", you need not even define that in your persistence.xml.

事实上,如果你的映射文件是“META-INF/orm.xml”,你甚至不需要在你的persistence.xml 中定义它。

This is what the hibernate configuration manual says about this:

这是 hibernate 配置手册对此的说明:

The class element specifies a EJB3 compliant XML mapping file that you will map. The file has to be in the classpath. As per the EJB3 specification, Hibernate EntityManager will try to load the mapping file located in the jar file at META_INF/orm.xml. Of course any explicit mapping file will be loaded too. As a matter of fact, you can provides any XML file in the mapping file element ie. either hbm files or EJB3 deployment descriptor.

class 元素指定您将映射的符合 EJB3 的 XML 映射文件。该文件必须在类路径中。根据 EJB3 规范,Hibernate EntityManager 将尝试加载位于 META_INF/orm.xml 的 jar 文件中的映射文件。当然,任何显式映射文件也将被加载。事实上,您可以在映射文件元素中提供任何 XML 文件,即。hbm 文件或 EJB3 部署描述符。

Reference: Hibernate configuration

参考:休眠配置

回答by Dmitri

Your orm.xml has a minor error in it that breaks the whole thing. The point is that the sequence of elements' declarations should comply with the respective XML schema (which is http://java.sun.com/xml/ns/persistence/orm_1_0.xsdin particular). That is easy to check by running the xml code through a validator which honours xml schemas. Below is the corrected version of your orm.xml that must work as a charm.

你的 orm.xml 有一个小错误,它破坏了整个事情。关键是元素声明的顺序应该符合各自的 XML 模式(特别是http://java.sun.com/xml/ns/persistence/orm_1_0.xsd)。通过使用支持 xml 模式的验证器运行 xml 代码,这很容易检查。以下是您的 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">
        <table name="Account" />
        <named-native-query name="myQuery" result-set-mapping="nicknames">
            <query><![CDATA[
            select a.nickname from Account a
            ]]></query>
        </named-native-query>
        <sql-result-set-mapping name="nicknames">
            <column-result name="nickname" />
        </sql-result-set-mapping>
    </entity>
</entity-mappings>