Java: How to map results of Hibernate Named SQL Query (in XML file) to an annotated Entity

Shaun Scovil picture Shaun Scovil · Jan 7, 2014 · Viewed 12.8k times · Source

I'm trying to map the results of a Hibernate Named SQL Query (stored in an XML file) to an annotated Entity object. I've read through countless docs and tutorials and support threads but I still can't seem to get it working.

Here's what I've got so far...

In my persistence.xml file, I've declared a mapping file called test.xml, and in test.xml I've got the following:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <sql-query name="Asset.Test">
        <![CDATA[ SELECT a.id, a.name FROM assets a WHERE a.id = 1 ]]>
    </sql-query>

</hibernate-mapping>

This works fine for retrieving results in a generic Object, using this code:

Object entity = session.getNamedQuery("Asset.Test").list().get(0);

But when I try to map these results to annotated Entity object, I keep getting an exception Caused by: org.hibernate.HibernateException: Errors in named queries: Asset.Test.

Here is my Entity:

import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.math.BigInteger;

@Entity
public class AssetTestEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private BigInteger id;
    private String name;

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

And here is my modified Named Query:

<sql-query name="Asset.Test">
    <return alias="assets" class="AssetTestEntity" />
    <![CDATA[ SELECT assets.id AS {assets.id}, assets.name AS {assets.name} FROM assets WHERE assets.id = 1 ]]>
</sql-query>

I've tried several variations of this, with and without the curly braces, using different aliases, etc.

I've RTFM, including this: http://docs.jboss.org/hibernate/stable/core.old/reference/en/html/querysql-namedqueries.html

I've seen dozens of examples like this: http://examples.javacodegeeks.com/enterprise-java/hibernate/hibernate-named-query-example/

Can anyone see what I'm doing wrong here? Do you have to define the entity in XML as well?

Thanks in advance!

UPDATE

I did manage to get it to work by defining the Entity class in the XML file, in addition to having the Entity object. The working mapping XML looks like this:

<hibernate-mapping>

    <class name="com.asset.AssetTestEntity" table="assets">
        <id name="id" type="java.math.BigInteger">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" />
        </property>
    </class>

    <sql-query name="Asset.Test">
        <return alias="assets" class="com.asset.AssetTestEntity" />
        <![CDATA[ SELECT a.id, a.name FROM assets a WHERE a.id = 1 ]]>
    </sql-query>

</hibernate-mapping>

I was hoping Hibernate could handle just using the annotated Entity without having to do this, but apparently not. I even tried adding @Column annotations to the entity...

Answer

Marcel St&#246;r picture Marcel Stör · Jan 7, 2014

The fact that it works if you define the OR mapping alongside the named query in the mapping files leads to a simple conclusion (for me): Hibernate doesn't know about your @Entity annotated class.

Make sure the persistence.xml lists your entity classes.