0

Please can you help me? In JPA, I try to create a OneToMany bidirectional relation, but I have the following errors : "[EclipseLink-63] : The instance creation method [entity.OrderLine.], with no parameters, does not exist, or is not accessible. [EclipseLink-28019] : Deployment of PersistenceUnit [simple-jpaPU] failed. Close all factories for this PersistenceUnit."

There are my entities : OneToMany Entity :

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 Entity :

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 Answers1

0

It fails because OrderLine does not have no-arg constructor. It is required as stated in JPA 2.1 specification (Chapter 2.1):

The entity class must have a no-arg constructor. The entity class may have other constructors as well.The no-arg constructor must be public or protected.

Default constructor is not generated because other constructor is given. Problem can be fixed by adding following constructor:

public OrderLine() {
}
Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • Whan I add the no-arg constructors in the two entities I have this errors : – Malik Berrada Mar 05 '17 at 12:13
  • When I add the no-arg constructors in the two entities I have this error : "[EclipseLink-4002] : org.eclipse.persistence.exceptions.DatabaseException Internal Exception: 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] Query: InsertObjectQuery(entity.Order[ id=151 ]) at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)" – Malik Berrada Mar 05 '17 at 16:25
  • Your second problem has nothing to do with no-arg constructor. My French is a bit rusty, but issue is that ORDER is reserved word in SQL. One way to solve problem is avoiding reserved word as table name. If that is not possible, two other ways to solve such a problem can be found from here: http://stackoverflow.com/questions/6791882/jpa-database-delimiters – Mikko Maunu Mar 05 '17 at 18:23
  • Thank you very much, I just changed the Order entity's name to Commande and it worked. – Malik Berrada Mar 05 '17 at 20:12