0

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)
}
aneto
  • 1,501
  • 2
  • 13
  • 19

2 Answers2

0

Looks like you want Cascading.

Refer to: http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Cascading

proulxs
  • 498
  • 2
  • 13
0

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.

Community
  • 1
  • 1
aneto
  • 1,501
  • 2
  • 13
  • 19