2017-02-25 1 views
0

도와주세요. JPA에서 OneToMany 양방향 관계를 만들려고하는데 다음과 같은 오류가 있습니다 : "[EclipseLink-63] : 매개 변수없이 [entity.OrderLine.] 인스턴스 생성 메서드가 존재하지 않거나 액세스 할 수 없습니다 [EclipseLink-28019] : PersistenceUnit [simple-jpaPU]의 배포가 실패했습니다.이 PersistenceUnit의 모든 공장을 닫으십시오. " OneToMany 엔티티 :JPA OneToMany 양방향 관계 [EclipseLink-63] 오류

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 엔티티 : OrderLine이 인수 없음의 생성자가 없기 때문에

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

답변

0

실패

내 실체가 있습니다. JPA 2.1 사양 (2.1 장)에서 설명한대로 필요합니다.

엔티티 클래스에는 인수가없는 생성자가 있어야합니다. 엔티티 클래스에는 다른 생성자도있을 수 있습니다. 인수가없는 생성자는 public 또는 protected이어야합니다.

다른 생성자가 주어지기 때문에 기본 생성자가 생성되지 않습니다. 나는이 오류가 두 엔티티의 인수 없음의 생성자를 추가

public OrderLine() { 
} 
+0

Whan : 나는 두 엔티티의 인수 없음의 생성자를 추가 할 때 –

+0

나는이 오류가있는 문제는 다음과 같은 생성자를 추가하여 해결할 수 있습니다 : "[EclipseLink-4002] : org.eclipse.persistence.exceptions.DatabaseException 내부 예외 : org.postgresql.util.PSQLException : 오류 : 구문 오류 : ORDER»위치 : 13 오류 코드 : 0 호출 : INSERT INTO ORDER (ID, CREATIONDATE) VALUES (?,?) bind => [2 매개 변수 경계] 질의 : InsertObjectQuery (entity.Order [id = 151]) at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl. 커밋 (EntityTransactionImpl.java:157) " –

+0

두 번째 문제는 없습니다. 인수가없는 생성자와 관련이 있습니다. 내 프랑스어는 약간 녹슬었지만 문제는 ORDER가 SQL에서 예약어라는 것입니다. 문제를 해결하는 한 가지 방법은 예약어를 테이블 이름으로 사용하지 않는 것입니다. 그게 가능하지 않다면,이 문제를 해결하는 다른 두 가지 방법은 여기에서 찾을 수 있습니다 : http://stackoverflow.com/questions/6791882/jpa-database-delimiters –