2010-08-09 239 views
0

我無法嘗試使用JPA映射原生SQL查詢,並將Hibernate作爲提供者進行映射。這是我使用用於此目的的實體代碼:將NativeQuery映射到實體

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",[email protected](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) { 
     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; 
    } 


} 

當我嘗試AS 5.1部署使用JBoss這個實體,我得到以下異常:

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' ** 

我真的不明白這個異常是什麼意思,並且我想要修改爲了讓Native sql映射到我的實體中?非常感謝。

+0

如果你可以發佈定義persistence-unit&session bean實現的xml,其中entityManager使用該單元。 – 2010-08-10 18:35:38

回答

0

嘗試使用小步驟,先獲取@SqlResultSetMapping權限,然後在常規java類中執行@NamedNativeQuery部分或使用createNativeQuery。

1

我想你需要告訴你的NamedNativeQuery它應該使用SqlResultSetMapping。你可以用以下方法做到這一點:

@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