开发者

Map a NativeQuery into an entity

开发者 https://www.devze.com 2023-01-10 13:57 出处:网络
I\'m having trouble trying to map a native SQL query using JPA, and Hibernate as a provider. This is the code for the entity that I\'m using for this purpose:

I'm having trouble trying to map a native SQL query using JPA, and Hibernate as a provider. This is the code for the entity that I'm using for this purpose:

package com.prueba.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.Id;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;

@Entity
@SqlResultSetMapping(name="CustomerOrderInfo",entities=@EntityResult(entityClass=com.prueba.entities.JoinEntity.class,
        fields={@FieldResult(name="id",column="id"),@FieldResult(name="title",column="title"),
                @FieldResult(name="firstName",column="firstName"),@FieldResult(name="lastName",column="lastName"),
                @FieldResult(name="orderId",column="oderId"),@FieldResult(name="shipping",column="shipping")}))
@NamedNativeQuery(name="CustomerOrderInfoJoin",query="SELECT c.customer_id as id,c.title as title ,c.fname as firstName,c.lname as lastName,"+
"o.orderinfo_id as orderId,"+
"o.shipping as shipping FROM customer c,orderinfo o WHERE"+
"c.customer_id=o.customer_id")
public class JoinEntity {
    @Id
    private Integer id;
    @Column
    private String title;
    @Column
    private String firstName;
    @Column
    private String lastName;
    @Column
    private Integer orderId;
    @Column
    private Double shipping;


    public Integer getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
   开发者_StackOverflow     this.lastName = lastName;
    }

    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public Double getShipping() {
        return shipping;
    }

    public void setShipping(Double shipping) {
        this.shipping = shipping;
    }


}

When I try to deploy this entity using JBoss AS 5.1, I get the following exception:

DEPLOYMENTS IN ERROR:
  Deployment "<UNKNOWN jboss.j2ee:jar=Prueba.jar,name=CustomerSessionImpl,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVED Demands 'persistence.unit:unitName=#pruebaPU' **
  Deployment "persistence.unit:unitName=#pruebaPU" is in error due to the following reason(s): org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
  Deployment "<UNKNOWN jboss.j2ee:jar=Prueba.jar,name=OrderInfoSessionImpl,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVED Demands 'persistence.unit:unitName=#pruebaPU' **

I really don't understand what does this exception mean, and also I wanted to as what should I modify in order to get that Native sql mapped into my entity?. Thanks a lot.


I think you need tell your NamedNativeQuery that it should use SqlResultSetMapping. You do that with the with the following:

    @NamedNativeQuery(name="CustomerOrderInfoJoin",query="SELECT c.customer_id as id,c.title as title ,c.fname as firstName,c.lname as lastName,"+
    "o.orderinfo_id as orderId,"+
    "o.shipping as shipping FROM customer c,orderinfo o WHERE"+
    "c.customer_id=o.customer_id", 
    resultSetMapping = "CustomerOrderInfo") <--- Like this


Try to use small steps, first get the @SqlResultSetMapping right, after that do the @NamedNativeQuery part or use createNativeQuery in your regular java class.

0

精彩评论

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