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>