Alexander
Alexander

Reputation: 7238

Hibernate - Many-To-Many with Roles

Consider the following model:

enter image description here

I do want to map this with JPA and Hibernate properly. At the moment the way I am doing this is by adding to lists to the User entitiy - CustomerContracts and ConsulterContracts.

@OneToMany(mappedBy = Contract.CUSTOMER_COLUMN, fetch=FetchType.EAGER,targetEntity=Contract.class)
private Set<Contract> customerContracts = new HashSet<Contract>();

@OneToMany(mappedBy = Contract.CUSTOMER_COLUMN, fetch=FetchType.EAGER,targetEntity=Contract.class)
private Set<Contract> consulterContracts = new HashSet<Contract>();

It's not very "handy" if you need to navigate through this model!

The problem is that I would prefer to have just one List at the user side - Contracts - but still have to user properties on the contract side (consulter and customer).

Is there a way do do this with hibernate and JPA

Upvotes: 0

Views: 113

Answers (1)

SJuan76
SJuan76

Reputation: 24885

There is no way to do it with ERD/SQL and, as JPA is just a mapping from object oriented programming to ERD, then no, there is no way.

You would need to change the ERD so there is a middle entity whose keys are UserId, ContractId and Role.

Upvotes: 1

Related Questions