我在尝试使用JPA和Hibernate作为提供程序映射本机SQL查询时遇到问题。这是我为此目的使用的实体的代码:
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) {
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;
}
}
当我尝试使用JBoss AS 5.1部署此实体时,我得到以下异常:
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 :(得分: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
答案 1 :(得分:0)
尝试使用小步骤,首先获取@SqlResultSetMapping,然后执行@NamedNativeQuery部分或在常规java类中使用createNativeQuery。