質問

UML図

国からスーパークラスの場所(@mappssuperclass)への関連性腫瘍が必要です。それは双方向かもしれません。私は@onetoanyのようなものが必要です...

@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;
}
}

国:

@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

@joincolunmがなければ、例外があります

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

テーブルシティと地域には、テーブルカントリー(Region.Country_id、City.Country_id)の外部キーが問題ありません。しかし、私はテーブルカントリーのテーブル地域と都市に外国のキーを必要としないので、@joincolumは必要ありません。

私はソリューションをたくさん探していましたが、それは良い解決策がないようです。

役に立ちましたか?

解決

@Any 外部キーがあるので、ここでは意味がありません PlaceS側であるため、追加のメタ列は必要ありません。

多型の関係を作成することが可能かどうかはわかりません @MappedSuperclass. 。ただし、宣言することもできます Place なので @Entity @Inheritance(InheritanceType.TABLE_PER_CLASS), 、同じデータベーススキーマを作成し、ポリモルピック関係を可能にする必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top