简体   繁体   English

Hibernate:使用@SqlResultSetMapping映射本机查询的结果集

[英]Hibernate: Mapping result set of native query with @SqlResultSetMapping

I'm trying the following: 我正在尝试以下方法:

MyResult.java : 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: 而在其他java类中:

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 [PersistenceUnit:MyHibernatePgSql]无法配置EntityManagerFactory

And when I remove the: "@Entity" part from the MyResult.java i get: 当我从MyResult.java中删除:“@ Entity”部分时,我得到:

org.hibernate.MappingException: Unknown SqlResultSetMapping [myResults] org.hibernate.MappingException:未知的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... 编辑:查询如下所示: 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? 你真的只得到[PersistenceUnit: MyHibernatePgSql] Unable to configure EntityManagerFactory没有任何堆栈跟踪或日志的情况下[PersistenceUnit: MyHibernatePgSql] Unable to configure EntityManagerFactory吗?

  • Does your entity have an @Id annotation somewhere (required for an entity)? 您的实体是否在某处具有@Id注释(实体需要)?

  • Why do you call getHibernateQuery ? 为什么调用getHibernateQuery This seems unnecessary and so does the cast into oheQueryImpl . 这似乎是不必要的,并且转换为oheQueryImpl

  • Why don't you use JPQL (in which case, you could use SELECT NEW assuming your entity does have the proper constructor)? 为什么不使用JPQL(在这种情况下,您可以使用SELECT NEW假设您的实体具有正确的构造函数)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM