6

我需要@OneToMany从 Country 到超类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(s):tour.spring.bc.model.vo.Country.places

在表 City 和 Region 中,表 Country (Region.country_id, City.country_id) 的外键是可以的。但是我不需要表 Country 到表 Region 和 City 中的外键,所以我不需要@JoinColum.

我能做些什么?

4

1 回答 1

6

@Any在这里没有意义,因为外键在Places 侧,因此不需要额外的元列。

我不确定是否可以创建与@MappedSuperclass. 但是,您可以尝试声明Place@Entity @Inheritance(InheritanceType.TABLE_PER_CLASS),它应该产生相同的数据库模式并允许多态关系。

于 2011-01-22T18:08:37.827 回答