2011-01-22 1 views
4

UML Diagram@MappedSuperclass과 @OneToMany

나는 (@MappedSuperclass를) 장소를 슈퍼 클래스에 국가 별 협회 OneToMany이 필요합니다. 그것은 양방향 일 수 있습니다. 나는 ... @OneToAny 같은 뭔가가 필요 wolud

@MappedSuperclass 
public class Place { 

private String name; 
private Country country; 

@Column 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

@ManyToOne 
@JoinColumn(name="country_id") 
public Country getCountry() { 
    return country; 
} 

public void setCountry(Country country) { 
    this.country = country; 
} 
} 

국가 :이 표시에서는 예외

Caused by: org.hibernate.AnnotationException: @Any requires an explicit @JoinColumn(s): tour.spring.bc.model.vo.Country.places 

이며, 지역 테이블 나라에 외래 키 @JoinColunm없이

@Entity 
    public class Country { 
    private long id; 
    private String name; 
    private List<Place> places; 

    @Any(metaColumn = @Column(name = "place_type"), fetch = FetchType.EAGER) 
    @AnyMetaDef(idType = "integer", metaType = "string", metaValues = { 
     @MetaValue(value = "C", targetEntity = City.class), 
     @MetaValue(value = "R", targetEntity = Region.class) }) 
    @Cascade({ org.hibernate.annotations.CascadeType.ALL }) 
    //@JoinColumn(name="unnecessary") 
    //@OneToMany(mappedBy="country") // if this, NullPointerException... 
    public List<Place> getPlaces() { 
     return places; 
    } 
//and rest of class 

(Region.country_id, City.country_id) 괜찮습니다. 그러나 @JoinColum이 필요하지 않으므로 테이블의 국가 및 도시에 외래 키가 필요하지 않습니다.

솔루션을 많이 찾고 있었지만 좋은 해결책이없는 것으로 보입니다.

답변

5

@Any 외래 키가 Place쪽에 있으므로 추가 메타 열이 필요하지 않으므로 여기서 의미가 없습니다.

@MappedSuperclass에 대한 다형성 관계를 만들 수 있는지 확실하지 않습니다. 그러나 Place@Entity @Inheritance(InheritanceType.TABLE_PER_CLASS)으로 선언하면 동일한 데이터베이스 스키마를 생성하고 다형성 관계를 허용해야합니다.

+0

데이터베이스 스키마에는 미묘하지만 중요한 차이가 있습니다. '@ MappedSuperclass'를 사용하면'Place '의 구체적인 하위 클래스마다 고유 한 ID 생성기를 가질 수 있지만'@Inheritance (InheritanceType.TABLE_PER_CLASS)'는 모두 동일해야합니다. 예를 들어,'Place'의 두 가지 하위 유형의 인스턴스는 이전의 경우 동일한 ID 번호를 가질 수 있지만 후자의 경우에는 가질 수 없습니다. – Devaro