@MappedSuperclass 和 @OneToMany

发布于 2024-10-13 09:13:17 字数 1638 浏览 4 评论 0原文

我需要从 Country 到超类 Place (@MappedSuperclass) 的关联 @OneToMany。它可以是双向的。我需要类似 @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

我能做些什么?

I need association @OneToMany from Country to superclass Place (@MappedSuperclass). It could be bidirectional. I would need something like @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;
    }
}

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

Without @JoinColunm there is an exception:

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

In table City and Region is foreign key to table Country (Region.country_id, City.country_id) which is ok.
But I do NOT need foreign key in table Country to tables Region and City so I don't need @JoinColum.

What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天气好吗我好吗 2024-10-20 09:13:17

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

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

@Any doesn't make sense here since foreign key is at the Places side and therefore it doesn't need additional meta column.

I'm not sure if it's possible to create a polymorphic relationship to @MappedSuperclass. However, you can try to declare Place as @Entity @Inheritance(InheritanceType.TABLE_PER_CLASS), it should produce the same database schema and allow polymorpic relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文