时间:2021-07-01 10:21:17 帮助过:2人阅读
在多对多保存测试的过程中,常常会出现以上错误信息,出现错误信息的原因是id值没有传进去,需要手动赋值。
如User和Role两个类是多对多关系,Role是维护端,User是维护端,具体代码:
package sys.entity; import java.io.Serializable; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; @Table(name="a_role") @Entity public class Role implements Serializable{ private Integer id; private String roleName; private Set<User>users=new HashSet<User>(); @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id==null?null:id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } @ManyToMany(mappedBy="roles") public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }
package sys.entity; import java.io.Serializable; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import sys.util.Constant; @Table(name="a_user") @Entity public class User implements Serializable{ private Integer id; private String username;//用户名 private String password;//密码 private Set<Role>roles=new HashSet<Role>(); @GeneratedValue(strategy=GenerationType.IDENTITY) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id==null?null:id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column(length=64) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @JoinTable(name="a_user_role",joinColumns={@JoinColumn(name="userId",referencedColumnName="id")}, inverseJoinColumns={@JoinColumn(name="roleId",referencedColumnName="id")}) @ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.EAGER) public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
@Test public void test6() { // 1、级联保存 Set<Role>roles=new HashSet<Role>(); Set<User>users=new HashSet<User>(); Role role=roleService.getRole(1); role.setUsers(users); roles.add(role); User user=new User(); //user.setId(-1); user.setUsername("李彤"); user.setPassword("1144"); user.setRoles(roles); users.add(user); userService.save(user); }
一测试就会出现以上错误!
4、解决方法。
解决的方法是设置一个Id值,任意设,如
user.setId(-1);
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案
标签: