2016-06-23 9 views
0

モデルにJPAアノテーションを追加しようとしています。ここでスーパークラスの双方向関係を持つJPAアノテーション

は私のスーパークラスの一部です:

@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(); 
} 

そして、これらは実装されている:

@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; 
    } 
} 

だから地域なしではなく都市でそれ都市、および国と地域を持っている国があります。

どのようにこれらのクラスに注釈を付けて、双方向関係を維持し、すべての宛先を単一の表にすることができますか?私の理解として理想的

答えて

0
  • デザインは、このよう

    国< ---->市

    国< ---->地域

    市<されている必要があります - - >地域

  • 地域が存在する可能性がある、または存在しないことを考慮すると、国から都市m地域に関連する必須およびオプションのマッピングを添付する。あなたが複数の値などについて

  • を持つことになります上に示し、現在のデザインあたりの手段として先に関連付けられているそれらのすべてを必要として、あなたの要件ごととして今

  • 。国(IND) - 地域(MH) - シティ(MUM);そして、あなたは目的地での2行国・都市およびその他の国・地域のための1

  • したがって、私の最終的な結論はあなたが継承デザインのために行く場合は、以前の例では、あなた場合で説明したように複数の行を持つことになりますということであるだろうここで示されているように、OneToOne/OneToManyマッピングを使用するだけで、都道府県と都道府県について1回2回続けます。

関連する問題