-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Unable to remove Many-to-Many association
PostPosted: Mon Dec 04, 2006 2:22 pm 
Newbie

Joined: Sat Aug 26, 2006 4:47 am
Posts: 9
Hibernate version: CORE 3.2.1GA, HEM 3.2.0.GA, HAN 3.2.0.GA


User.java
Code:
// default package
// Generated Dec 1, 2006 11:13:24 PM by Hibernate Tools 3.2.0.beta8

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
* User generated by hbm2java
*/
@Entity
@Table(name = "User", catalog = "jpa", uniqueConstraints = {})
public class User implements java.io.Serializable {

    // Fields

    /**
     *
     */
    private static final long serialVersionUID = 4832158639325710373L;

    private String id;

    private String login;

    private String passwd;

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

    // Constructors

    /** default constructor */
    public User() {
    }

    /** minimal constructor */
    public User(String id, String login, String passwd) {
        this.id = id;
        this.login = login;
        this.passwd = passwd;
    }

    /** full constructor */
    public User(String id, String login, String passwd, Set<Role> roles) {
        this.id = id;
        this.login = login;
        this.passwd = passwd;
        this.roles = roles;
    }

    // Property accessors
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true, length = 32)
    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name = "login", unique = false, nullable = false, insertable = true, updatable = true, length = 32)
    public String getLogin() {
        return this.login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @Column(name = "passwd", unique = false, nullable = false, insertable = true, updatable = true, length = 32)
    public String getPasswd() {
        return this.passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    @ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
    @JoinTable(name = "UserRole", joinColumns = @JoinColumn(name = "userid", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "roleid", referencedColumnName = "id"))
    public Set<Role> getRoles() {
        return this.roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof User)) {
            return false;
        }
        return this.login.equals(((User) o).login);
    }

    public int hashCode() {
        return 0;
    }
}


Role.java
Code:
// default package
// Generated Dec 1, 2006 11:13:24 PM by Hibernate Tools 3.2.0.beta8

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
* Role generated by hbm2java
*/
@Entity
@Table(name = "Role", catalog = "jpa", uniqueConstraints = {})
public class Role implements java.io.Serializable {

    // Fields

    /**
     *
     */
    private static final long serialVersionUID = -8599860702857703898L;

    private String id;

    private String role;

    private Set<User> users = new HashSet<User>(0);

    // Constructors

    /** default constructor */
    public Role() {
    }

    /** minimal constructor */
    public Role(String id, String role) {
        this.id = id;
        this.role = role;
    }

    /** full constructor */
    public Role(String id, String role, Set<User> users) {
        this.id = id;
        this.role = role;
        this.users = users;
    }

    // Property accessors
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true, length = 32)
    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name = "role", unique = false, nullable = false, insertable = true, updatable = true, length = 32)
    public String getRole() {
        return this.role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
    public Set<User> getUsers() {
        return this.users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Role)) {
            return false;
        }
        return this.role.equals(((Role) o).role);
    }

    public int hashCode() {
        return 0;
    }

}


TestCase.java
Code:
...
    public void testUpdateUser() throws Exception {
        User user = userDao.findById("402881830f4e6643010f4e664d1a0001");
        Role role = user.getRoles().iterator().next();
        role.getUsers().remove(user);
        user.getRoles().remove(role);
        userDao.merge(user);
    }
...


I have 3 tables, User, Role and UserRole, the last one is a join table. The above testcase does not trigger a DELETE statement to remove the association in table UserRole. Am I missing something ?! Thanks for your kindly help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.