Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 为此查找操作提供了不正确PK类的实例_Jpa - Fatal编程技术网

Jpa 为此查找操作提供了不正确PK类的实例

Jpa 为此查找操作提供了不正确PK类的实例,jpa,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 : clas

在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;
    }*/



}
主要类别:

 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>

org.eclipse.persistence.jpa.PersistenceProvider
实体.客户
实体.地址
entity.OnlineCustomer
entity.CustomerSingle
entity.OnlineCustomerJoined
entity.CustomerJoined
entity.OnlineCustomerTable
entity.CustomerTable
实体.秩序
实体发票
在这种情况下

@Id 
@Column(name = "ORDER_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;
实体的键是long,甚至是long

您正在尝试使用自动装箱为整数的int参数查找用户

Order=em.find(Order.class,111)

这就是为什么当您提供了一个不正确的PK类错误实例时,您会得到

试用

Order-Order=em.find(Order.class,111l)//在末尾以小写形式添加L

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