更新oneToMany实体将父项设置为null [英] Updating oneToMany entity sets parent to null
问题描述
在用户
实体上有一个角色
实体的列表,该实体与 @ OneToMany
关系。当我更改 User
实体上的列表,即更改角色时,hibernate删除旧角色并根据需要添加新角色。然而,外键字段在角色
实体上设置为空。
你能说明为什么foregign关键字段是空的,我将如何改变它,所以它会得到更新?
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)
私人用户用户;
表前
+ ---- + ------ + ------------------------ +
| id |角色|用户|
+ ---- + ------ + ------------------------ +
| 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 |
+ ---- + ------ + ------------------------ +
表格后
+ ---- + ------ + ------------------------ +
| id |角色|用户|
+ ---- + ------ + ------------------------ +
| 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动作 Hibernate:选择userroles0_.user为user3_2_0_,userroles0_.id为id1_1_0_,userroles0_.id为id1_1_1_,userroles0_.role为role2_1_1_,userroles0_.user为user3_1_1_,来自角色userroles0_,其中userroles0_.user =?
Hibernate:插入角色(角色,用户)值(?,?)
Hibernate:插入角色(角色,用户)值(?,?)
Hibernate:插入角色角色,用户)值(?,?)
Hibernate:插入角色(角色,用户)值(?,?)
Hibernate:插入角色(角色,用户)值(?,?)
Hibernate:插入角色(角色,用户)值(?,?)
Hibernate:更新用户设置first_name =?,last_name =?,middle_name =?,organisation_id =?哪里电子邮件=?
Hibernate:从角色删除其中id =?
Hibernate:从角色删除其中id =?
Hibernate:从角色删除其中id =?
Hibernate:从角色删除其中id =?
Hibernate:从角色删除其中id =?
Hibernate:从角色删除其中id =?
Hibernate:从角色删除其中id =?
Hibernate:从角色删除其中id =?
更新
在注释中调用数据库的方法
@Override
@Transactional
public User更新(用户用户)抛出UserNotFoundException {
字符串updatedUserEmail = user.getEmail();
if(userRepository.findByEmail(updatedUserEmail)== null)
throw new UserNotFoundException();
返回userRepository.save(user);
}
@Transactional
public interface UserRepository扩展CrudRepository< User,Long> {
感谢您提前给予任何帮助。
我使用EntityManager并使用Hibernate来完成这项工作,它似乎工作正常。您是否将旧角色从
列表< Role>用户:
@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>();
角色:
@Entity
public class Role {
@Id @GeneratedValue private Long id;
@ManyToOne
私人用户用户;
运行它:
User u = em.find(User.class,test@test.com);
角色e =新角色();
e.setUser(u);
u.getUserRoles()。remove(0);
u.getUserRoles()。add(e);
em.merge(u);
给我:
Hibernate:insert into Role(user_email,id)values(?,?)
Hibernate:从Role中删除id =?
Hibernate:select * from role
而在角色表中只有一行: / p>
[2,test@test.com]
I have a list of
Role
entities on aUser
entity linked with a@OneToMany
relationship. When I change the list on theUser
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 theRole
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.
解决方案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]
这篇关于更新oneToMany实体将父项设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文