简体   繁体   English

JPA ManyToMany仅坚持一侧

[英]JPA ManyToMany persist only one side

I have the table User in relation of ManyToMany with Radio like the tables below. 我有与多对多关系与无线电的表User,如下表。 In this relation only the method saveUser persist in relationship table. 在此关系中,只有方法saveUser保留在关系表中。 To summarize only the dominant side is persisted in USER and RADIO_USER table. 总而言之,USER和RADIO_USER表中仅保留了主导方。 How I persist the other side? 我如何坚持另一边?

table USER: 表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: 表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. 这是因为关系是双向的,对于Hibernate,所有者端是用来了解关联存在的那一侧。 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. 有关更多详细信息,请参见@ManyToMany(mappedBy =“ foo”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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