JPA OneToMany双向关系[EclipseLink-63]错误

时间:2017-02-25 10:35:49

标签: one-to-many entitymanager jpa-2.1

你能帮助我吗?在JPA中,我尝试创建OneToMany双向关系,但是我有以下错误: " [EclipseLink-63]:没有参数的实例创建方法[entity.OrderLine。]不存在,或者不可访问。 [EclipseLink-28019]:PersistenceUnit [simple-jpaPU]的部署失败。关闭此PersistenceUnit的所有工厂。"

有我的实体: OneToMany实体:

package entity;
import java.util.*;
import java.io.Serializable;
import javax.persistence.*;
import org.eclipse.persistence.annotations.TimeOfDay;

@Entity
public class Order implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;    
    @OneToMany(mappedBy = "o")
    private   List<OrderLine>  orderLines; 

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public List<OrderLine> getOrderLines() {
        return orderLines;
    }

    public void setOrderLines(ArrayList<OrderLine> orderLines) {
        this.orderLines = orderLines;
    }

    public Order(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
    return "entity.Order[ id=" + id + " ]";
    }
}

ManyToOne实体:

package entity;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name="orderline_table")
public class OrderLine implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String item; 
    private  Double unitPrice;   
    private Integer quantity;
    @ManyToOne
    Order o; 

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public Double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(Double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public OrderLine(String item, Double unitPrice, Integer quantity) {
        this.item = item;
        this.unitPrice = unitPrice;
        this.quantity = quantity;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }


    @Override
    public String toString() {
        return "entity.OrderLine[ id=" + id + " ]";
    }    
}

1 个答案:

答案 0 :(得分:0)

失败是因为OrderLine没有no-arg构造函数。如JPA 2.1规范(第2.1章)中所述,这是必需的:

  

实体类必须具有无参数构造函数。实体类也可以有其他构造函数.no-arg构造函数必须是public或protected。

未生成默认构造函数,因为给出了其他构造函数。可以通过添加以下构造函数来解决问题:

public OrderLine() {
}