Database:
I have a three-way connection table called Users_Accounts_Roles
.
+--------------+------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+------------+------+-----+-------------------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | user_id | bigint(20) | NO | MUL | NULL | | | account_id | bigint(20) | NO | | 0 | | | role_id | bigint(20) | NO | | NULL | | +--------------+------------+------+-----+-------------------+----------------+
Users can belong to several accounts and can have several roles for each of these accounts.
I also have a User
table
+----------------+--------------+------+-----+---------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------------------+-----------------------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | email | varchar(255) | NO | UNI | NULL | | | firstName | varchar(255) | NO | | NULL | | | lastName | varchar(255) | NO | | NULL | | +----------------+--------------+------+-----+---------------------+-----------------------------+
Account
table
+--------------+-------------+------+-----+---------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------------------+-----------------------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | name | varchar(50) | NO | | NULL | | +--------------+-------------+------+-----+---------------------+-----------------------------+
And the Role
table
+--------------+--------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------------------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | description | text | NO | | NULL | | +--------------+--------------+------+-----+---------------------+----------------+
Objects (rarely):
@Embeddable AccountRole { ... @Parent User getUser() { return user; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "account_id") Account getAccount() { return account; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "role_id") Role getRole() { return role; } ... } @Entity User { ... @Transient Set<Account> getAccounts() { return accounts; } @ElementCollection(fetch = FetchType.EAGER) @JoinTable(name = "Users_Accounts_Roles", joinColumns = @JoinColumn(name = "user_id")) Set<AccountRole> getAccountRoles() { return accountRoles; } ... } @Entity Account { ... @Transient Set<User> users; ... }
I want User.accounts
to be User.accounts
with data in Users_Accounts_Roles
when the User
is retrieved from the database, but I do not want User.accounts
changes User.accounts
affect updates to Users_Accounts_Roles
when the User
saved. Similarly, I want Account.users
be populated with data in Users_Accounts_Roles
when the account is retrieved from the database, but I do not want changes in Accounts.users
affect updates to Users_Accounts_Roles
when the Account
saved. The only way the Users_Accounts_Roles
table should change is if a User
saved with the updated accountRoles
field.
As with the case, the User.accountRoles
display works to my taste (both are retrieved from and saved until Users_Accounts_Roles
), but I cannot find a way for User.accounts
and Account.users
to get to User
and Account
, but they are not saved when User
or Account
without using some ugly logic in the DAO layer. (They are currently marked as transient, as there is nothing more than I tried to work on). Does Hibernate / JPA support what I'm trying to do?
----- EDIT -----
I suspect my decision may include using @OneToMany(mappedBy="...")
on User.accounts
and Account.users
, as done in this guide . However, I cannot figure out how to comment on the fields in AccountRole
and the User.accountRoles
field to make sure that the changes to the last are saved.