Setting sleep mode to retrieve a collection without saving it

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.

+4
source share
1 answer

You can try matching user.accounts with @ManyToMany using @JoinTable(name ="Users_Accounts_Roles", joinColumns=...) . By default, @*ToMany does not cascade anything, so you do not run the risk of adding / removing entries by mistake. However, a change in the Account themselves will be saved, but this is only due to the fact that they are managed objects. In any case, those Account objects will be the same as those associated with the user.accountRoles collections, so I think this should not be a problem.

Take care of the possibilities of such a solution, since this can lead to a large number of requests (for example, one additional request for each user to retrieve his accounts).

Alternatively, you can simply implement a shell (read-only) Collection<Account> , which delegates all its calls to the user.accountRoles collection (just calls getAccount() on the AccountRole objects that it returns). Thus, you can control each operation and ensure that the ratio is selected only once.

0
source

Source: https://habr.com/ru/post/1383759/


All Articles