Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
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


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


Java
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?
Posted
Comments
Shubhashish_Mandal 15-Jul-13 2:14am    
delete from database or from ORM ?
saigowthami 15-Jul-13 2:19am    
It is getting deleted from database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900