[ACCEPTED]-define named query in orm.xml with jpa and hibernate-named-query
Ok I finally got it !
I was saving my orm.xml 6 in META-INF directory. When I move this 5 file to my package where I have my domain 4 object (like in my example: com.mysite), the 3 orm.xml is not ignored and all run.
I also 2 need to change the path in mapping-file 1 (persistence.xml) : com/mysite/orm.xml
Your orm.xml has a minor error in it that 8 breaks the whole thing. The point is that 7 the sequence of elements' declarations should 6 comply with the respective XML schema (which 5 is http://java.sun.com/xml/ns/persistence/orm_1_0.xsd in particular). That is easy to check 4 by running the xml code through a validator 3 which honours xml schemas. Below is the 2 corrected version of your orm.xml that must 1 work as a charm.
<?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>
As said
When I try to create my named query 14 with em.createNamedQuery("myQuery"), it returns that it can not find this query.
You 13 are right. But you forget the following
If 12 you place a named query definition inside 11 a element, instead of the root, it is prefixed with the name of the entity class
So you 10 need to call your namedQuery as
em.createNamedQuery("Account.myQuery")
I am curious: Does 9 your Account class is stored in the root 8 classpath ??? If not, you have fix its missing 7 package. Suppose Account class is stored 6 inside br.com.hibernate.model.domain.Account. So 5 you should declare your entity as
<entity class="br.com.hibernate.model.domain.Account" instead
And you 4 need to call your namedQuery as
em.createNamedQuery("br.com.hibernate.model.domain.Account.myQuery") instead
Just an adivice: when 3 you are using Hibernate as your Persistence 2 Provider, you do not need to define your 1 Entity class in persistence.xml file.
regards,
"META-INF/orm.xml" is the default 16 mapping file that will be looked upon by 15 any JPA compliant entity manager.
In fact, if 14 your mapping file is "META-INF/orm.xml", you 13 need not even define that in your persistence.xml.
This 12 is what the hibernate configuration manual 11 says about this:
The class element specifies 10 a EJB3 compliant XML mapping file that you 9 will map. The file has to be in the classpath. As 8 per the EJB3 specification, Hibernate EntityManager 7 will try to load the mapping file located 6 in the jar file at META_INF/orm.xml. Of 5 course any explicit mapping file will be 4 loaded too. As a matter of fact, you can 3 provides any XML file in the mapping file 2 element ie. either hbm files or EJB3 deployment 1 descriptor.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.