1 Reply Latest reply: May 23, 2012 6:32 AM by Adam Warski RSS

collection is not associated with any session

G K Newbie

Hello Community,

 

I am sorry for my bad english, but i try my best ;-)

 

I am developing an application to learn Hibernate. I have got a class called "User" which contains n objects of class "Role" and class Role contains n objects of class "Rule". Everything is in lazyLoad mode.

 

Here is class: "User"

@Entity
@Table(name="users")
public class User implements Serializable 
{
    @Id
    @Column
    private String userId;
    @Column
    private String userPassword;
    @Column
    private Date registrationDate;
    @Column
    private Date lastLogin;
    @Column
    private boolean online;
    @ManyToMany(targetEntity=Role.class)
    private List<Role> roles = new ArrayList<Role>();
    
    public void addRole(Role r) {
        Hibernate.initialize(roles);
        this.roles.add(r);
    }
    public List<Role> getRoles() {
        return roles;
    }
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getUserPassword() {
        return userPassword;
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    public Date getRegistrationDate() {
        return registrationDate;
    }
    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }
    public Date getLastLogin() {
        return lastLogin;
    }
    public void setLastLogin(Date lastLogin) {
        this.lastLogin = lastLogin;
    }
    public boolean isOnline() {
        return online;
    }
    public void setOnline(boolean online) {
        this.online = online;
    }

 

Here is class Role

 

@Entity
@Table(name="Roles")
public class Role implements Serializable {
    @Column
    private String roleName;
    @Id
    @Column
    private String roleId;
    @ManyToMany(targetEntity=Rule.class)
    private List<Rule> rules = new ArrayList<Rule>();
    
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    public String getRoleId() {
        return roleId;
    }
    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }
    public List<Rule> getRuleList() {
        return rules;
    }
    public void setRuleList(List<Rule> ruleList) {
        this.rules = ruleList;
    }
    public void addRule(Rule r) {
        Hibernate.initialize(rules);
        this.rules.add(r);
    }

 

And last one...class Rule

 

@Entity
@Table(name="rules")
public class Rule implements Serializable {
    @Id
    @Column
    private String ruleId;
    @Column
    private String ruleName;
    @Column
    private String ruleValue;
    
    public String getRuleId() {
        return ruleId;
    }
    public void setRuleId(String ruleId) {
        this.ruleId = ruleId;
    }
    public String getRuleName() {
        return ruleName;
    }
    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }
    public String getRuleValue() {
        return ruleValue;
    }
    public void setRuleValue(String ruleValue) {
        this.ruleValue = ruleValue;
    }

 

 

When I start my webservice and send an message to my service with the method "addRuleToRole"...I want that my Role add an existing Rule. I looked it up in my database there are an object of Role and Rule. But my service failed with the response:

 

org.apache.cxf.interceptor.Fault: collection is not associated with any session

 

Can anyone help me?