Updating oneToMany entity sets parent to null

假如想象 提交于 2019-12-08 13:00:27

问题


I have a list of Role entities on a User entity linked with a @OneToMany relationship. When I change the list on the User entity, i.e change the roles, hibernate removes the old roles and adds the new ones as desired. However the foreign key field is set to null on the Role entity.

Can you explian why the foregign key field is null, and how would I change it so it gets picked up on an update?

User.java

@SerializedName("userrole")
@Expose
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Role>   userRoles = new ArrayList<Role>();

Role.java

@ManyToOne
@JoinColumn(name = "user")
private User user;

Table Before

+----+------+------------------------+
| id | role | user                   |
+----+------+------------------------+
|  1 |    0 | digby.tyson@gmail.com  |
|  2 |    1 | digby.tyson@gmail.com  |
|  3 |    2 | digby.tyson@gmail.com  |
|  4 |    3 | digby.tyson@gmail.com  |
|  5 |    4 | digby.tyson@gmail.com  |
|  6 |    5 | digby.tyson@gmail.com  |
|  7 |    6 | digby.tyson@gmail.com  |
|  8 |    7 | digby.tyson@gmail.com  |
|  9 |    5 | ronny.polley@gmail.com |
| 10 |    6 | ronny.polley@gmail.com |
| 11 |    7 | reed.robert@gmail.com  |
+----+------+------------------------+

Table After

+----+------+------------------------+
| id | role | user                   |
+----+------+------------------------+
|  9 |    5 | ronny.polley@gmail.com |
| 10 |    6 | ronny.polley@gmail.com |
| 11 |    7 | reed.robert@gmail.com  |
| 12 |    0 | NULL                   |
| 13 |    1 | NULL                   |
| 14 |    2 | NULL                   |
| 15 |    5 | NULL                   |
| 16 |    6 | NULL                   |
| 17 |    7 | NULL                   |
+----+------+------------------------+

Hibernate actions

Hibernate: select userroles0_.user as user3_2_0_, userroles0_.id as id1_1_0_, userroles0_.id as id1_1_1_, userroles0_.role as role2_1_1_, userroles0_.user as user3_1_1_ from role userroles0_ where userroles0_.user=?
Hibernate: insert into role (role, user) values (?, ?)
Hibernate: insert into role (role, user) values (?, ?)
Hibernate: insert into role (role, user) values (?, ?)
Hibernate: insert into role (role, user) values (?, ?)
Hibernate: insert into role (role, user) values (?, ?)
Hibernate: insert into role (role, user) values (?, ?)
Hibernate: update user set first_name=?, last_name=?, middle_name=?, organisation_id=? where email=?
Hibernate: delete from role where id=?
Hibernate: delete from role where id=?
Hibernate: delete from role where id=?
Hibernate: delete from role where id=?
Hibernate: delete from role where id=?
Hibernate: delete from role where id=?
Hibernate: delete from role where id=?
Hibernate: delete from role where id=?

Update

The method that calls the database as requested in comments

@Override
@Transactional
public User update(User user) throws UserNotFoundException {
    String updatedUserEmail = user.getEmail();

    if (userRepository.findByEmail(updatedUserEmail) == null)
        throw new UserNotFoundException();

    return userRepository.save(user);

}

@Transactional
public interface UserRepository extends CrudRepository<User, Long> {

Thanks for any help in advance.


回答1:


Mapping is correct, now what you want to achieve is when removing a Role from User, you want that role to be deleted (orphanRemoval = true).

This is a Bidirectional Association, so what you have to do is make the changes in both sides to keep your object model consistent :

// remove role from user one-to-many
user.getUserRoles().remove(roleToBeDeleted);
// remove the many-to-one association
roleToBeDeleted.setUser(null);



回答2:


I did this with EntityManager and using Hibernate and it seems to be working OK. Are you taking the old role out of the List<Role> userRoles?

User:

@Entity
public class User {
    @Id private String email;

    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Role> userRoles = new ArrayList<Role>();

Role:

@Entity
public class Role {
    @Id @GeneratedValue private Long id;

    @ManyToOne
    private User user;

running it as:

User u = em.find(User.class, "test@test.com");
Role e = new Role();
e.setUser(u);
u.getUserRoles().remove(0);
u.getUserRoles().add(e);
em.merge(u);

Gives me:

Hibernate: insert into Role (user_email, id) values (?, ?)
Hibernate: delete from Role where id=?
Hibernate: select * from role

And in the roles table is only one row:

[2, test@test.com]


来源:https://stackoverflow.com/questions/36050088/updating-onetomany-entity-sets-parent-to-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!