首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@MappedSuperclass和@OneToMany

@MappedSuperclass和@OneToMany
EN

Stack Overflow用户
提问于 2011-01-22 18:00:06
回答 1查看 11K关注 0票数 7

我需要联系@OneToMany从国家到超级阶级Place (@MappedSuperclass)。可能是双向的。我需要像@OneToAny这样的东西。

代码语言:javascript
复制
@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;
    }
}

国家/地区:

代码语言:javascript
复制
@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

在餐桌城市和地区是外键的表国家(Region.country_id,City.country_id),这是可以的。但我不需要外键在表国家表,区域和城市,所以我不需要@JoinColum

我能做什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-22 18:08:37

@Any在这里没有意义,因为外键位于Place的旁边,因此它不需要额外的元列。

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4769546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档