Hibernation: Matching the result of dialing your own query using @SqlResultSetMapping

I am trying to do 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 another 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] Failed to configure EntityManagerFactory

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

org.hibernate.MappingException: Unknown SqlResultSetMapping [myResults]

I know that I am doing something wrong, but I do not know what? Also I can not find good documentation about this.

Thanks in advance

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

+3
source share
1 answer

Some comments / questions:

  • [PersistenceUnit: MyHibernatePgSql] Unable to configure EntityManagerFactory ?

  • @Id - ( )?

  • getHibernateQuery? , o.h.e.QueryImpl.

  • JPQL ( SELECT NEW, , )?

+3

Source: https://habr.com/ru/post/1744784/


All Articles