6

@OneToManyCountry からスーパークラスPlace( ) への関連付けが必要@MappedSuperclassです。双方向である可能性があります。のようなものが必要です@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はありません:

原因: org.hibernate.AnnotationException: @Any には明示的な @JoinColumn が必要です: tour.spring.bc.model.vo.Country.places

テーブル City と Region はテーブル Country (Region.country_id, City.country_id) への外部キーであり、これは問題ありません。しかし、テーブルRegionとCityへのテーブルCountryには外部キーは必要ないので、必要ありません@JoinColum

私に何ができる?

4

1 に答える 1

6

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

とのポリモーフィックな関係を作成できるかどうかはわかりません@MappedSuperclassPlaceただし、 as として宣言することはでき@Entity @Inheritance(InheritanceType.TABLE_PER_CLASS)ます。同じデータベース スキーマを生成し、多相関係を許可する必要があります。

于 2011-01-22T18:08:37.827 に答える