简体   繁体   English

具有超类双向关系的JPA批注

[英]JPA annotations with bidirectional relationship of superclass

I'm trying to add JPA annotations to my model. 我正在尝试向模型添加JPA批注。

Here's a part of my superclass: 这是我超类的一部分:

@Entity
@Table(name="destinations")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Destination {
    protected abstract Destination getParent();
    protected abstract Set<Destination> getChildren();
}

And these are the implementations: 这些是实现:

@Entity
@DiscriminatorValue(value = "country")
public class Country extends Destination {
    private Set<Destination> regions;
    private Set<Destination> cities;

    @Override
    public Destination getParent() {
        return null;
    }

    @Override
    public Set<Destination> getChildren() {
        if (regions != null && !regions.isEmpty()) {
            return regions;
        }
        return cities;
    }
}

@Entity
@DiscriminatorValue(value = "region")
public class Region extends Destination {
    private Set<Destination> cities;
    private Country country;

    @Override
    public Destination getParent() {
        return country;
    }

    @Override
    public Set<Destination> getChildren() {
        return cities;
    }
}

@Entity
@DiscriminatorValue(value = "city")
public class City extends Destination {
    private Country country;
    private Region region;

    @Override
    public Destination getParent() {
        if (region != null) {
            return region;
        }
        return country;
    }

    @Override
    public Set<Destination> getChildren() {
        return null;
    }
}

So there are Countries that have Regions with Cities in it, and Countries without Regions but with Cities. 因此,有一些国家中有城市的区域,也有没有区域但有城市的国家。

How do I annotate these classes so their bidirectional relationships can be persist and all the Destinations are in a single table? 如何注释这些类,以便它们的双向关系可以持久存在,并且所有“目标”都在一个表中?

  • Ideally as my understanding the design should have been this way 理想情况下,根据我的理解,设计应该是这种方式

    Country <----> City 国家<---->城市

    Country <----> Region 国家<---->地区

    City <----> Region 城市<---->地区

  • Considering that region can be or cannot exist we need to have Country to City mapping mandatory and optional mapping related to Region. 考虑到该区域可能存在或不存在,我们需要将“国家/地区到城市”映射强制性和与区域相关的可选映射。

  • Now as per your requirement where in you need all of them associated to Destination which means as per the current design shown above you will have multiple values 现在,根据您的需求,在其中您需要将所有它们与“目标”相关联,这意味着根据上面显示的当前设计,您将拥有多个值

  • for eg. 例如 Country(IND)-Region(MH)-City(MUM); 国家(IND)-地区(MH)-城市(MUM); Then you would 2 rows in destination one for Country-City and other Country-Region 然后,您将在目标国家/城市和其他国家/地区的目标位置两行

  • Hence my final conclusion is that if you go for Inheritance Design you will end up having multiple rows as explained in the previously example and if you simply use OneToOne/OneToMany mapping as shown in point you persist twice once for Country-City and Country-Region 因此,我的最终结论是,如果您进行继承设计,您将最终有多行,如前面的示例中所述,并且如果您仅使用OneToOne / OneToMany映射(如要点所示),则对Country-City和Country-Region会保留两次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM