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)これは大丈夫です。 しかし、私はテーブルCountryとTableに外部キーは必要ないので、@ JoinColumは必要ありません。
私は解決策をたくさん探していましたが、良い解決策はないようです。
データベースのスキーマには微妙ではあるが重要な違いがあります。 '@ MappedSuperclass'では、' Place'の具象サブクラスはそれぞれ独自のIDジェネレータを持つことができますが、 '@Inheritance(InheritanceType.TABLE_PER_CLASS)'ではすべてが同じでなければなりません。例えば、 'Place'の2つの異なるサブタイプのインスタンスは、前者の場合には同じID番号を持つことができますが、後者の場合はできません。 – Devaro