2016-08-17 10 views
1

CにAのインスタンスへの参照を与えたいが、すべてのA hasはBのインスタンスに対する外部キーである。BはCのインスタンスに対する外部キーを持つ。以下はその例である。Hibernateの3番目のエンティティを介して1つのエンティティを2番目のエンティティに参加させるにはどうすればよいですか?

@Entity 
public Class A { 

    public int id; 
    public Integer bId; 

} 

@Entity 
    public Class B { 
    public Integer id; 
    public Integer cId; 
} 


@Entity 
    public Class C { 
    public A a; 
    public Integer id; 
} 

どのように私はこの目標を達成するために春のHibernateアノテーションを使用していますか?

答えて

1

私は最小限の実装は(JPA 2.xのを使用して)次のようになりますと思う:

@Entity 
class A { 
    public Integer id; 
    public Integer bId; 
} 

@Entity 
class B { 
    public Integer id; 
    public Integer cId; 

    @OneToMany 
    @JoinColumn(name="bId") 
    private Set<A> a; 
} 

@Entity 
class C { 

    public Integer id; 
    @ManyToOne 
    public A a; 

    @OneToMany 
    @JoinColumn(name="cId") 
    private Set<B> b; 

} 

それとも、実体参照の代わりのidのに依存する場合:

@Entity 
class A { 
    public Integer id; 

    @ManyToOne 
    public B b; 
} 

@Entity 
class B { 
    public Integer id; 

    @ManyToOne 
    public C c; 
} 

@Entity 
class C { 
    public Integer id; 

    @ManyToOne 
    public A a; 
} 
関連する問題