Search code examples
javahibernatejpahibernate-annotations

JPA using one join table for two OneToMany entity


There's an Entity Class 'A' (supposed to be a Person),There's another Entity Class 'B' (supposed to be a Contract).

Entity 'A' has a relation @OneToMany to Class 'B' ( a person can sign alot of contracts). Entity 'B' also has a relation @OneToMany to Class 'A' (a contract can have many person signing it).

In this case, there's gonna be 2 JoinTable in database, but actually they both are somehow the same.

Is there anyway that i make them just using One JoinTable?

tnx for any help!


Solution

  • Looks like a @ManyToMany relation to me...

    in class Person

    @ManyToMany
    @JoinTable(name="PERS_CONTRACTS")
    public Set<Contract> getContracts() { return contracts; }
    

    in class Contract

    @ManyToMany(mappedBy="contracts")
    public Set<Person> getSigners() { return signers; }