为此查找操作提供了错误PK类的实例

时间:2014-04-24 02:46:13

标签: jpa

我在JPA中实现一对一关系时面临以下问题。 我想获得第111号订单的发票详情。

错误:

 Exception in thread "main" java.lang.IllegalArgumentException: You have provided an instance of an incorrect PK class for this find operation.  Class expected : class java.lang.Long, Class received : class java.lang.Integer.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.findInternal(EntityManagerImpl.java:695)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:619)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:498)
    at test.OneToOneTest.main(OneToOneTest.java:16)
Java Result: 1

实体类--- ORDER.java: 该应用程序将需要解决订单发票关系。对于每个订单,都会有一张发票;并且,类似地,每张发票与订单相关联。这两个表与一对一映射相关。

package entity;

import java.util.Date;

import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

/*
 * Order Entity - maps to ORDERS table
 */

@Entity(name = "ORDERS") 
public class Order {

@Id 
@Column(name = "ORDER_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;

@Column(name = "CUST_ID",insertable=false, updatable=false)
private long custId;

@Column(name = "TOTAL_PRICE", precision = 2)
private double totPrice;

@Column(name = "OREDER_DESC")
private String orderDesc;

@Column(name = "ORDER_DATE") 
     @Temporal(TemporalType.TIMESTAMP)
private Date orderDt;   

@OneToOne(optional=false,cascade=CascadeType.ALL, mappedBy="order",targetEntity=Invoice.class)
private Invoice invoice;



@ManyToOne(optional=false)
    @JoinColumn(name="CUST_ID",referencedColumnName="CUST_ID")
    private Customer customer;


    /*@ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="ORDER_DETAIL",
            joinColumns=
                @JoinColumn(name="ORDER_ID", referencedColumnName="ORDER_ID"),
            inverseJoinColumns=
                @JoinColumn(name="PROD_ID", referencedColumnName="PROD_ID")
    )
    private List<Product> productList;*/


    @Column(name = "LAST_UPDATED_TIME")
         @Temporal(TemporalType.TIMESTAMP)
    private Date updatedTime;


    public String toString() {
       StringBuffer sb = new StringBuffer();
       sb.append("orderId : " + orderId);
       sb.append("   custId : " + custId);
       sb.append("   totPrice : " + totPrice);
       sb.append("   orderDesc : " + orderDesc);
       sb.append("   orderDt : " + orderDt);
       //sb.append("   invoice : " + invoice);
       //sb.append("   products : " + productList);
       return sb.toString();
   }

    public long getCustId() {
        return custId;
    }

    public void setCustId(long custId) {
        this.custId = custId;
    }

    public String getOrderDesc() {
        return orderDesc;
    }

    public void setOrderDesc(String orderDesc) {
        this.orderDesc = orderDesc;
    }

    public Date getOrderDt() {
        return orderDt;
    }

    public void setOrderDt(Date orderDt) {
        this.orderDt = orderDt;
    }

    public long getOrderId() {
        return orderId;
    }

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

    public double getTotPrice() {
        return totPrice;
    }

    public void setTotPrice(double totPrice) {
        this.totPrice = totPrice;
    }

    public Date getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(Date updatedTime) {
        this.updatedTime = updatedTime;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /*public List<Product> getProductList() {
        return productList;
    }

    public void setProductList(List<Product> productList) {
        this.productList = productList;
    }*/



}

Invoice.java: 显示的Invoice实体将INVOICE表的所有字段映射为属性,并使用ORDER_ID外键连接Order对象。

    package entity;

import java.util.Date;

import javax.persistence.*;

/*
 * INVOICE Entity - maps to ORDER_INVOICE table
 */
@Entity(name = "ORDER_INVOICE") 
public class Invoice {

    @Id //signifies the primary key
    @Column(name = "INVOICE_ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long invoiceId;

    @Column(name = "ORDER_ID",insertable=false, updatable=false)
    private long orderId;

    @Column(name = "AMOUNT_DUE", precision = 2)
    private double amountDue;

    @Column(name = "DATE_RAISED") 
          @Temporal(TemporalType.TIMESTAMP)
    private Date orderRaisedDt;

    @Column(name = "DATE_SETTLED") 
          @Temporal(TemporalType.TIMESTAMP)
    private Date orderSettledDt;

    @Column(name = "DATE_CANCELLED")
          @Temporal(TemporalType.TIMESTAMP)
    private Date orderCancelledDt;


    @Column(name = "LAST_UPDATED_TIME")
          @Temporal(TemporalType.TIMESTAMP)
    private Date updatedTime;

    @OneToOne(optional=false)
    @JoinColumn(name = "ORDER_ID") 
    private Order order;

    public String toString() {
       StringBuffer sb = new StringBuffer();
      // sb.append("orderId : " + orderId);
       sb.append("   invoiceId : " + invoiceId);
       sb.append("   amtDue : " + amountDue);
       sb.append("   orderRaisedDt : " + orderRaisedDt);
       sb.append("   orderSettledDt : " + orderSettledDt);
       sb.append("   orderCancelledDt : " + orderCancelledDt);
       sb.append("   updatedTime : " + updatedTime);
       return sb.toString();
   }


    public Date getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(Date updatedTime) {
        this.updatedTime = updatedTime;
    }


    public long getInvoiceId() {
        return invoiceId;
    }


    public void setInvoiceId(long invoiceId) {
        this.invoiceId = invoiceId;
    }


    public Date getOrderRaisedDt() {
        return orderRaisedDt;
    }


    public void setOrderRaisedDt(Date orderRaisedDt) {
        this.orderRaisedDt = orderRaisedDt;
    }


    public Order getOrder() {
        return order;
    }


    public void setOrder(Order order) {
        this.order = order;
    }


    public long getOrderId() {
        return orderId;
    }


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


    public double getAmountDue() {
        return amountDue;
    }


    public void setAmountDue(double amountDue) {
        this.amountDue = amountDue;
    }


    public Date getOrderCancelledDt() {
        return orderCancelledDt;
    }


    public void setOrderCancelledDt(Date orderCancelledDt) {
        this.orderCancelledDt = orderCancelledDt;
    }


    public Date getOrderSettledDt() {
        return orderSettledDt;
    }


    public void setOrderSettledDt(Date orderSettledDt) {
        this.orderSettledDt = orderSettledDt;
    }
}

主要课程:

 package test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import entity.Order;

public class OneToOneTest {

    public static void main(String[] args) {
        EntityManagerFactory entityManagerFactory =  Persistence.createEntityManagerFactory("TESTJPAPU");

        EntityManager em = entityManagerFactory.createEntityManager();

        Order order = em.find(Order.class, 111);
        em.close();
        entityManagerFactory.close();
        System.out.println("order : " + order);

    }
}

的persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="TESTJPAPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>entity.Customer</class>
    <class>entity.Address</class>    
    <class>entity.OnlineCustomer</class>
    <class>entity.CustomerSingle</class>  
    <class>entity.OnlineCustomerJoined</class>
    <class>entity.CustomerJoined</class>
    <class>entity.OnlineCustomerTable</class>
    <class>entity.CustomerTable</class>
    <class>entity.Order</class>
    <class>entity.Invoice</class>


<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/CUSTOMERJPA?zeroDateTimeBehavior=convertToNull"/>
  <property name="javax.persistence.jdbc.password" value=""/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

  <property name="eclipselink.create-ddl-jdbc-file-name" value="createDDL_ddlGeneration.jdbc"/> 
  <property name="eclipselink.drop-ddl-jdbc-file-name" value="dropDDL_ddlGeneration.jdbc"/> 
  <property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>

1 个答案:

答案 0 :(得分:1)

在这种情况下

@Id 
@Column(name = "ORDER_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;

实体的关键是长期甚至是长期。

您正在尝试使用autobox到Integer的int参数找到用户。

  

订单订单= em.find(Order.class,111);

这就是你得到的原因你提供了一个错误的PK类错误的实例

尝试使用

  

订单订单= em.find(Order.class,111l); //最后在小写字母中添加L.