I am trying to create a named-native-query that returns a Long.
Here is my orm.xml file (simplified as much as possible)
<?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 orm_2_0.xsd"
version="2.0">
<named-native-query name="getCaseNumberByCommId" result-class="java.lang.Long">
<query>SELECT case_id FROM communications WHERE comm_id =(?1)</query>
</named-native-query>
</entity-mappings>
this is the error I get:
ERROR - org.hibernate.impl.SessionFactoryImpl - Error in named query: getCaseNumberByCommId [coral:launch] org.hibernate.MappingException: Unknown entity: java.lang.Long
I've also tried just specifying "Long"
<named-native-query name="getCaseNumberByCommId" result-class="Long">
<query>SELECT case_id FROM communications WHERE comm_id =(?1)</query>
</named-native-query>
and strangely get this error:
Caused by: org.hibernate.AnnotationException: Unable to find entity-class: Long ... Caused by: java.lang.ClassNotFoundException: Long
Java can't find Long in java.lang?
Thank you for any clues
edit: I tried removing the 'result-class' annotation :
<named-native-query name="getCaseNumberByCommId" >
<query>SELECT case_id FROM communications WHERE comm_id =(?1)</query>
</named-native-query>
and get this error:
nested exception is org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
Update*
I never did find a way to do this, but since the database had a uniqueness constraint on comm_id, I was able to just return a mapped pojo object instead of a count.
e.g.
<named-native-query name="getByCommId" result-class="com.foo.model.Communication">
<query>SELECT * FROM communications WHERE comm_id =(?1)</query>
</named-native-query>
and then pull the desired case_id out of the returned pojo.
Without looking into this further the result-class
should be one of your mapped entities
and because Long
is not one of your Entities you're receiving the AnnotationException
.
Try removing the result-class
from the mapping as it is only used for building queries that return Entity
Objects.
Edit:
In regard to:
org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
:
After a quick dig around I found this:
https://hibernate.atlassian.net/browse/ANN-661
Where I read:
Workaround is to do such queries using Session.createSQLQuery() or straight JDBC.
There are a couple of examples for such queries here: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html#d0e17378
i.e
sess.createSQLQuery("SELECT * FROM CATS").list();
sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").list();
or further down in Example 18.4
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html#querysql-namedqueries
<sql-query name="mySqlQuery">
<return-scalar column="name" type="string"/>
<return-scalar column="age" type="long"/>
SELECT p.NAME AS name,
p.AGE AS age,
FROM PERSON p WHERE p.NAME LIKE 'Hiber%'
</sql-query>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With