繁体   English   中英

在对集合进行get调用时,Hibernate会删除Lazy加载的多对多集合

[英]Hibernate deletes Lazy loaded many-to-many collection when a get call is made on the collection

我试图在java应用程序中使用hibernate从数据库中获取角色。 我正在进行多对多的映射。

获取数据后,它将从数据库中删除。 我没有调用删除方法仍然发生删除。

Role.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;
}

}

User.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的单独问题,还是我只是更新这个问题并希望找到合适的解决方案?

我想这是一个问题

private Set<Role> roles = new HashSet<Role>(0);

@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;
}

它应该是

 private Set<Role> roles;

@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() {
    if(roles == null){
         return new HashSet<Role>;
    }
    return this.roles;
}

因为每当调用getRoles时,它总是返回一个空集。 所以hibernate假设您实际上已经清空/删除了该集并在数据库中删除它们

暂无
暂无

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

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