【问题标题】:JPA OneToMany bidirectional relation [EclipseLink-63] errorJPA OneToMany 双向关系 [EclipseLink-63] 错误
【发布时间】:2017-02-25 10:35:49
【问题描述】:

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

有我的实体: 一对多实体:

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 + " ]";
    }
}

多对一实体:

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 + " ]";
    }    
}

【问题讨论】:

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


    【解决方案1】:

    它失败是因为OrderLine 没有无参数构造函数。如 JPA 2.1 规范(第 2.1 章)所述,它是必需的:

    实体类必须有一个无参数的构造函数。实体类也可能有其他构造函数。无参数构造函数必须是公共的或受保护的。

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

    public OrderLine() {
    }
    

    【讨论】:

    • 当我在两个实体中添加无参数构造函数时,我有这个错误:
    • 当我在两个实体中添加无参数构造函数时,出现此错误:“[EclipseLink-4002] : org.eclipse.persistence.exceptions.DatabaseException 内部异常:org.postgresql.util。 PSQLException: ERREUR: erreur de syntaxe sur ou près de « ORDER » Position : 13 Error Code: 0 Call: INSERT INTO ORDER (ID, CREATIONDATE) VALUES (?, ?) bind => [2 parameters bound] 查询: InsertObjectQuery(entity .Order[ id=151 ]) 在 org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)"
    • 您的第二个问题与无参数构造函数无关。我的法语有点生疏,但问题是 ORDER 是 SQL 中的保留字。解决问题的一种方法是避免保留字作为表名。如果这是不可能的,可以从这里找到解决此类问题的其他两种方法:stackoverflow.com/questions/6791882/jpa-database-delimiters
    • 非常感谢,我刚刚将 Order 实体的名称更改为 Commande,并且成功了。
    猜你喜欢
    • 1970-01-01
    • 2011-06-22
    • 2018-11-29
    • 1970-01-01
    • 2013-11-09
    • 2017-06-23
    • 2013-06-17
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多