I am trying to get roles from a database using hibernate in a java application. I turn to many cards for the same thing.
After retrieving the data, it is deleted from the database. I do not name the delete method, the removal is still happening.
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; } }
We are trying to get roles from the database using the following code snippet
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; }
after extracting the roles, the data (corresponding role) is deleted at the same time, can someone tell me why?
Edit - . We are debugging eclipse, and hibernation is a collection to be deleted, since currentPersistor becomes null for the collection object. Debug further and publish any update.
Change 1 . We missed the mention that the User was @Cacheable and was retrieved from ehcache. When the getRole call was run, the collection will be loaded, and then prompty will be queued for deletion. The problem with @Cacheable annotation fixes the problem. Am I creating a separate question regarding @Cacheable and manytomany, or am I just updating this question and hope for a correct solution?