2012-04-09 3 views
11

JPAeclipselink을 사용하고 있습니다. 엔티티가 있습니다. 복합 키은 두 개의 필드로 구성됩니다. 다음은 내 Embeddable 기본 키 클래스 필드 (구성원)입니다.Embeddable 클래스 내의 외래 키 매핑

@Embeddable 
    public class LeavePK { 
     @ManyToOne(optional = false) 
     @JoinColumn(name = "staffId", nullable = false) 
     private Staff staff; 
     @Temporal(TemporalType.TIMESTAMP) 
     private Calendar date; 
     //setters and getters 
    } 

내 엔티티는 직원 관련 데이터를 남겨 개최 할 예정입니다, 그래서 직원 개체를 결합하여 복합 키를 생성하는 일을두고 노력하고 있어요. 내 논리와는 별도로, 임베디드 클래스 내에 외래 키 매핑을 허용하지 않습니다. JPA 도구 -> 엔티티에서 테이블 생성을 사용하려고하면 다음과 같이 오류가 발생하지만 설명하지는 않습니다.

org.eclipse.persistence.exceptions.ValidationException 
Exception Description: The mapping [staff] from the embedded ID class [class rs.stapp.entity.LeavePK] is an invalid mapping for this class. An embeddable class that is used with an embedded ID specification (attribute [leavePK] from the source [class rs.stapp.entity.Leave]) can only contain basic mappings. Either remove the non basic mapping or change the embedded ID specification on the source to be embedded. 

은 내가 또한 외래 키 (복합 키에서) 키를 가질 수 없습니다, 뜻. 이 ERM을 수행 할 수있는 다른 방법이 있습니까? 도와주세요. 감사합니다

답변

12

@IdClass 또는 @EmbeddedId 사람에 대한 관계도 ID 클래스에 넣지 마십시오. @Embeddable 클래스는 @Basic, @Column, @Temporal, @Enumerated, @Lob 또는 @Embedded과 같은 주석 만 포함 할 수 있습니다. 다른 모든 것은 공급자 별 구문입니다 (예 : Hibernate가 허용하지만, JPA RI 인 EclipseLink를 사용하고 있으므로 이것이 원하는 것입니다).

@Entity 
@Table(name = "Zips") 
public class Zip implements Serializable 
{ 
    @EmbeddedId 
    private ZipId embeddedId; 

    @ManyToOne 
    @JoinColumn(name = "country_code", referencedColumnName = "iso_code") 
    private Country country = null; 

    ... 
} 

@Embeddable 
public class ZipId implements Serializable 
{ 
    @Column(name = "country_code") 
    private String countryCode; 

    @Column(name = "code") 
    private String code; 

    ... 
} 

HTH

: 여기

는 예를 JPA PK/FK 매핑입니다