2

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...

Shaun Scovil
  • 3,905
  • 5
  • 39
  • 58
  • The one obvious difference between the first named query and the second is the presence of parameters. Can you provide the full stacktrace and the code used to invoke the second named query? – Marcel Stör Jan 07 '14 at 19:52
  • Yeah, sorry about that. Fixed the example code, which I was trying to keep simple...but it still doesn't work. – Shaun Scovil Jan 07 '14 at 20:02
  • "I was hoping Hibernate could handle just using the annotated Entity" - it definitely can as the Hibernate docs you link to show. – Marcel Stör Jan 07 '14 at 20:31

2 Answers2

1

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.

Community
  • 1
  • 1
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • Hey, thanks for the response! In my `persistence.xml` file I have set `` to auto-detect entities. Not sure why that wouldn't be working... – Shaun Scovil Jan 07 '14 at 21:05
  • You're definitely on to something here. I tried adding a `` tag for my entity in `persistence.xml` and now I'm getting a different exception, probably related to some incorrect annotation in my entity. Thanks again! – Shaun Scovil Jan 07 '14 at 21:14
  • You're welcome. Feel free to post follow-up questions and reference them here (rather than appending follow-ups here). Also, if the original problem is fixed consider accepting this answer. – Marcel Stör Jan 07 '14 at 23:41
0

Hibernate can also automatically populate beans from query results:

List<AssetTestEntity> results = session.getNamedQuery("Asset.Test")
       .setResultTransformer(Transformers.aliasToBean(AssetTestEntity.class))
       .list();

However the results are not "managed" like non-native query results.

Wayne Baylor
  • 159
  • 3