简体   繁体   中英

How to write named queries with xml in Hibernate/JPA?

I am able to use Named Query using annotations without any issues. But when I try to switch to orm.xml files to externalize my application couldn't find my named query which is in User.orm.xml file under classpath META-INF directory. The full path of my User.orm.xml file is src\\main\\resources\\META-INF\\User.orm.xml When I try to run the query I get the following exception,

java.lang.IllegalArgumentException: No query defined for that name [READ_ALL_USERS]
[artifact:mvn]  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.buildQueryFromName(AbstractEntityManagerImpl.java:788)

My persistence.xml which is under src/main/resources/META-INF looks like this,

<?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" 
             version="2.0">

    <persistence-unit name="webPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.spring.model.User</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/spring?zeroDateTimeBehavior=convertToNull"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="password"/>
        </properties>
    </persistence-unit>

</persistence>

And my User.orm.xml file has the following named-query,

<?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">  
    <named-query name="READ_ALL_USERS">
        <query>SELECT user from com.spring.User user</query>
    </named-query>
</entity-mappings>

I looked upon this question which suggests to keep the orm.xml file where the model class exists. But it doesn't work for me. What is the problem in the above code.

I've also included the mapping the User.orm.xml file in my persistence.xml which is already in my classpath under META-INF directory. I've tried the following file mappings but none of them detects my xml file,

<mapping-file>User.orm.xml</mapping-file>

<mapping-file>/META-INF/User.orm.xml</mapping-file>

<mapping-file>classpath:User.orm.xml</mapping-file>

You must add a <mapping-file> element inside the <persistence-unit> element.

Example: <persistence-unit> <mapping-file>User.orm.xml</mapping-file> </persistence-unit>

Source: https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM