开发者

Hibernate: Mapping result set of native query with @SqlResultSetMapping

开发者 https://www.devze.com 2022-12-30 06:16 出处:网络
I\'m trying the following: MyResult.java : import java.io.Serializable; import javax.persistence.Column;

I'm trying the following:

MyResult.java :

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.SqlResultSetMapping;


@Entity
@SqlResultSetMapping(name = "myResults", entities = {@EntityResult(entityClass = MyResult.class)})
public class MyResult implements Serializable
{

    /**
     * 
     */
    private static final long   serialVersionUID    = -1934790337160508576L;

    @Column(name="X")
    private int x;

    @Column(name="Y")
    private double y;


    // 
    // Getters and Setters...
    //
}

And in other java class:

Query q = ((org.hibernate.ejb.QueryImpl开发者_运维技巧) this.entityManager.createNativeQuery (this.sql,
                        "myResults")).getHibernateQuery ( );
List<MyResult> result = q.list ( );

When I run this code I get:

[PersistenceUnit: MyHibernatePgSql] Unable to configure EntityManagerFactory

And when I remove the: "@Entity" part from the MyResult.java i get:

org.hibernate.MappingException: Unknown SqlResultSetMapping [myResults]

I know that I'm doing something wrong but I don't know what? Also I can't find good documentation about this.

Thanks in advance

edit: The query looks like this: SELECT X, AGG_FUNC(F) AS Y FROM...


Some remarks/questions:

  • Do you really get only [PersistenceUnit: MyHibernatePgSql] Unable to configure EntityManagerFactory without any stack trace or log?

  • Does your entity have an @Id annotation somewhere (required for an entity)?

  • Why do you call getHibernateQuery? This seems unnecessary and so does the cast into o.h.e.QueryImpl.

  • Why don't you use JPQL (in which case, you could use SELECT NEW assuming your entity does have the proper constructor)?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号