我正在尝试在 java 应用程序中使用 hibernate 从数据库中获取角色。我正在为此进行多对多映射。
获取数据后,它将从数据库中删除。我也没有调用 delete 方法,仍然会发生删除。
角色.java
@Entity
@Table(name = "ROLE")
public class Role extends BaseModel implements java.io.Serializable {
private Long id;
private Set<User> users = new HashSet<User>(0);
@Id
@Column(name = "ID", unique = true, nullable = false, scale = 0)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
@JoinTable(name = "USERROLEMAP", joinColumns = { @JoinColumn(name = "ROLEID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "USERID", nullable = false, updatable = false) })
public Set<User> getUsers() {
return this.users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
用户.java
@Cacheable
@Entity
@Table(name = "USER", uniqueConstraints = @UniqueConstraint(columnNames = "LOGINID"))
public class User extends BaseModel implements java.io.Serializable {
private Long id;
private Set<Role> roles = new HashSet<Role>(0);
@Id
@Column(name = "ID", unique = true, nullable = false, scale = 0)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "USERROLEMAP", joinColumns = { @JoinColumn(name = "USERID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "ROLEID", nullable = false, updatable = false) })
public Set<Role> getRoles() {
return this.roles;
}
}
我们正在尝试使用以下代码片段从数据库中获取角色
public List<String> rolesAsGA() {
List<String> proxyUserRoles = new ArrayList<String>();
Iterator<Role> itr = getRoles().iterator();
while (itr.hasNext()) {
proxyUserRoles.add(new SimpleGrantedAuthority(itr.next()
.getRoleName()));
}
return proxyUserRoles;
}
获取角色后,数据(相应角色)同时被删除,谁能告诉我为什么?
编辑 -我们在 eclipse 中调试,hibernate 将集合标记为删除,因为集合实体的 currentPersistor 变为 null。将进一步调试并发布任何更新。
编辑 1我们错过了提及,用户是 @Cacheable,并且正在从 ehcache 中获取,当进行 getRole 调用时,集合被加载,然后及时排队等待删除。删除 @Cacheable 注释可以解决问题。我是否会创建一个关于 @Cacheable 和 manytomany 的单独问题,或者我是否应该更新这个问题并希望得到适当的解决方案?