简体   繁体   中英

JPA ManyToMany persist only one side

I have the table User in relation of ManyToMany with Radio like the tables below. In this relation only the method saveUser persist in relationship table. To summarize only the dominant side is persisted in USER and RADIO_USER table. How I persist the other side?

table USER:

+--------------------+--------------+------+-----+-------------------+----------------+
| Field              | Type         | Null | Key | Default           | Extra          |
+--------------------+--------------+------+-----+-------------------+----------------+
| user_account_id    | bigint(20)   | NO   | PRI | NULL              | auto_increment |
| name               | varchar(255) | YES  |     | NULL              |                |
+--------------------+--------------+------+-----+-------------------+----------------+

table RADIO:

+-----------------------+---------------+------+-----+-------------------+----------------+
| Field                 | Type          | Null | Key | Default           | Extra          |
+-----------------------+---------------+------+-----+-------------------+----------------+
| radio_id              | bigint(20)    | NO   | PRI | NULL              | auto_increment |
| name                  | varchar(128)  | NO   |     | NULL              |                |
+-----------------------+---------------+------+-----+-------------------+----------------+

table RADIO_USER:

+--------------------+------------+------+-----+-------------------+----------------+
| Field              | Type       | Null | Key | Default           | Extra          |
+--------------------+------------+------+-----+-------------------+----------------+
| account_radio_id   | bigint(20) | NO   | PRI | NULL              | auto_increment |
| radio              | bigint(20) | YES  | MUL | NULL              |                |
| user_account       | bigint(20) | YES  | MUL | NULL              |                |
+--------------------+------------+------+-----+-------------------+----------------+

USER Entity:

@ManyToMany
@JoinTable(name = "RADIO_USER",
    joinColumns = {@JoinColumn(name = "user_account", referencedColumnName = "user_account_id")},
    inverseJoinColumns = {@JoinColumn(name = "radio", referencedColumnName = "radio_id")})
List<Radio> radios;

@Transactional
public void saveUser(User user) {
    user.setRadios(radioDao.getAll(Radio.class));
    entityManager.persist(user);
}

RADIO Entity:

@ManyToMany(mappedBy="radios")
List<User> users;

@Transactional
public void saveRadio(Radio radio) {
    radio.setUsers(userDao.getAllUsersByRole(User.Role.ROLE_ADMIN));
    entityManager.persist(radio)
}

That is because the relationship is bidirectional and for Hibernate the owner side is the side which is used to know that association exists. Then for this I need use this code:

@Transactional
public void save(Radio radio) {
    List<User> users = userDao.getAllUsersByRole(User.Role.ROLE_ADMIN);

    for (User user : users) {
        user.getRadios().add(radio);
    }

    radio.setUsers(users);
    entityManager.persist(radio)
}

See @ManyToMany(mappedBy = “foo”) for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM