I am trying to fetch roles from database using hibernate in java application. I am undergoing a many to many mapping for the same.
After fetching the data, it is getting deleted from database. I am not calling the delete method either still the deletion occurs.
The 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
@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 the roles from 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 fetching the roles the data (corresponding role) is getting deleted simultaneously, can anyone tell me why?