2017-01-02 6 views
-1

もう一度私のプロジェクトを手伝ってもらう必要があります。関係を避ける方法 "私の父は私の息子です"

私の関係のクラス:

private GT_Member mother; 

private GT_Member father; 

private GT_Family ownerF; 

private List<GT_Member> children; 

がどのように検証親子Springアプリケーションに実装できますか?私は彼の息子として一人の父または祖父を置くことを禁じたい。

クライアントのlourdは単純な状況ですが、私はリストをフィルタリングします。しかし、それはどのようにデータベースで行うのですか?

答えて

1

addChildメソッドを作成し、階層が再帰的に上向きになっていることを確認して、子が自分の祖先に含まれていないことを確認します。このよう

public class GT_Member { 

    private final GT_Member mother; 

    private final GT_Member father; 

    private final List<GT_Member> children; 

    public GT_Member(final GT_Member mother, final GT_Member father) { 
     this.mother = mother; 
     this.father = father; 
     this.children = new ArrayList<>(); 
    } 

    /** 
    * Add a child to this member. 
    * @param child 
    */ 
    public void addChild(GT_Member child) { 
     if (child == null) { 
      throw new IllegalArgumentException("Null children not allowed"); 
     } 
     if (child.equals(this)) { 
      throw new IllegalArgumentException("Can't add self as a child"); 
     } 
     if (isAncestor(child)) { 
      throw new IllegalArgumentException("Can't add an ancestor as a child"); 
     } 
     children.add(child); 
    } 

    /** 
    * Check if a member is an ancestor to this member. 
    * @param member 
    * @return True if the member is an ancestor, false otherwise 
    */ 
    private boolean isAncestor(final GT_Member member) { 
     // Check if the member is the mother or father 
     return member.equals(mother) || member.equals(father) 
       // Check if the member is an ancestor on the mother's side 
       || (mother != null && mother.isAncestor(member)) 
       // Check if the member is an ancestor on the father's side 
       || (father != null && father.isAncestor(member)); 
    } 
} 
+0

感謝たくさん。これだよ。 – VANILKA

0

私は2つのレベルでチェックにアプローチします 1.アプリケーションコードのビジネスロジック検証。 2.このような不適合関係が作成/更新されないようにするためのデータベーストリガ。

+0

私はetc.etc ..私は私のいとこは私の息子ではないか、私の妻は私の娘ではないことも確認しなければならないため、ビジネスロジックでそれを行うprefere ... – VANILKA

関連する問題