【问题标题】:Map a NativeQuery into an entity将 NativeQuery 映射到实体中
【发布时间】:2011-03-27 02:24:24
【问题描述】:

我在尝试使用 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' **

我真的不明白这个异常是什么意思,而且我想我应该修改什么以便将本机 sql 映射到我的实体中?非常感谢。

【问题讨论】:

  • 如果您可以发布定义持久性单元和会话 bean 实现的 xml,其中 entityManager 使用了该单元。

标签: hibernate jpa mapping native


【解决方案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

【讨论】:

    【解决方案2】:

    尝试使用小步骤,首先正确设置@SqlResultSetMapping,然后执行@NamedNativeQuery 部分或在常规java 类中使用createNativeQuery。

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 2018-12-09
      相关资源
      最近更新 更多