Results 1 to 4 of 4

Thread: Cannot update or add to a collection using lazy=true

  1. #1

    Default Cannot update or add to a collection using lazy=true

    I can add, update and delete a record in the User table but I cannot add or update to the collection that is related to that user record.

    When I delete a user all the roles related to that user get deleted too (which is what I expected it to do).

    Yet, why can I not add or update the collection (roles) of a user?

    The relationship of User to Role table is one-to-many and is set to lazy=true.

    Can someone help? I just want to be able to add a new user and update an existing user such that the roles will get added or updated too.

    Thank you

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    theone,

    could you provide a small and runnable code so I can reproduce and debug this issue? You can send it to me off-list if you like.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3

    Default

    Thought I would post the related files here...
    again, my problem is i cannot add or update the collections (which is members in this case) when a user is added or modified.
    deleting a user is no problem all related members got deleted as they should. What could be the problem? Can somebody help?

    <hibernate-mapping>
    <class
    name="com.ibm.cruser.model.User" table="users">

    <id name="id" type="java.lang.Integer" column="id" unsaved-value="0" >
    <generator class="increment" />
    </id>

    <property name="username" type="java.lang.String" column="username" not-null="true" length="30" >
    </property>
    <property name="password" type="java.lang.String" column="password" not-null="true" length="30">
    </property>

    other properties....

    <!-- Associations -->

    <!-- bi-directional one-to-many association to Member -->
    <set
    name="members" lazy="true" inverse="true" cascade="save-update">
    <key>
    <column name="userid" />
    </key>
    <one-to-many
    class="com.ibm.cruser.model.Member" />
    </set>

    </class>
    </hibernate-mapping>


    <hibernate-mapping>
    <class
    name="com.ibm.cruser.model.Group"
    table="groups"
    >

    <id
    name="id"
    type="java.lang.Integer"
    column="id"
    >
    <generator class="assigned" />
    </id>

    <property
    name="groupname"
    type="java.lang.String"
    column="groupname"
    not-null="true"
    length="30"
    >
    </property>

    <!-- Associations -->

    <!-- bi-directional one-to-many association to Member -->
    <set
    name="members"
    lazy="true"
    inverse="true"
    cascade="all"
    >
    <key>
    <column name="groupid" />
    </key>
    <one-to-many
    class="com.ibm.cruser.model.Member"
    />
    </set>

    </class>
    </hibernate-mapping>

    <hibernate-mapping>
    <class
    name="com.ibm.cruser.model.Member"
    table="member"
    >

    <composite-id name="comp_id" class="com.ibm.cruser.model.MemberPK">
    <meta attribute="field-description" inherit="false">
    @hibernate.id
    generator-class="assigned"
    </meta>
    <key-property
    name="userid"
    column="userid"
    type="java.lang.Integer"
    length="4"
    >
    <meta attribute="field-description">
    @hibernate.property
    column="userid"
    length="4"
    </meta>
    </key-property>
    <key-property
    name="groupid"
    column="groupid"
    type="java.lang.Integer"
    length="4"
    >
    <meta attribute="field-description">
    @hibernate.property
    column="groupid"
    length="4"
    </meta>
    </key-property>
    </composite-id>


    <!-- Associations -->
    <!-- derived association(s) for compound key -->
    <!-- bi-directional many-to-one association to Group -->
    <many-to-one
    name="group"
    class="com.ibm.cruser.model.Group"
    update="false"
    insert="false"
    >
    <meta attribute="field-description">
    @hibernate.many-to-one
    update="false"
    insert="false"
    </meta>
    <meta attribute="field-description">
    @hibernate.column
    name="groupid"
    </meta>
    <column name="groupid" />
    </many-to-one>

    <!-- bi-directional many-to-one association to User -->
    <many-to-one
    name="user"
    class="com.ibm.cruser.model.User"
    update="false"
    insert="false"
    >
    <meta attribute="field-description">
    @hibernate.many-to-one
    update="false"
    insert="false"
    </meta>
    <meta attribute="field-description">
    @hibernate.column
    name="userid"
    </meta>
    <column name="userid" />
    </many-to-one>

    <!-- end of derived association(s) -->


    </class>
    </hibernate-mapping>

    public class User implements Serializable {

    /** identifier field */
    private Integer id;

    /** persistent field */
    private String username;

    /** persistent field */
    private String password;

    With other properties, getters and setters, constructors...

    }

    public class Group implements Serializable {

    /** identifier field */
    private Integer id;

    /** persistent field */
    private String groupname;

    /** persistent field */
    private Set members;

    With getters, setters and constructors
    }

    public class Member implements Serializable {

    /** identifier field */
    private com.ibm.cruser.model.MemberPK comp_id;

    /** nullable persistent field */
    private com.ibm.cruser.model.Group group;

    /** nullable persistent field */
    private com.ibm.cruser.model.User user;

    /** full constructor */
    public Member(com.ibm.cruser.model.MemberPK comp_id, com.ibm.cruser.model.Group group, com.ibm.cruser.model.User user) {
    this.comp_id = comp_id;
    this.group = group;
    this.user = user;
    }

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

    /** minimal constructor */
    public Member(com.ibm.cruser.model.MemberPK comp_id) {
    this.comp_id = comp_id;
    }

    /**
    * @hibernate.id
    * generator-class="assigned"
    *
    */
    public com.ibm.cruser.model.MemberPK getComp_id() {
    return this.comp_id;
    }

    public void setComp_id(com.ibm.cruser.model.MemberPK comp_id) {
    this.comp_id = comp_id;
    }

    /**
    * @hibernate.many-to-one
    * update="false"
    * insert="false"
    *
    * @hibernate.column
    * name="groupid"
    *
    */
    public com.ibm.cruser.model.Group getGroup() {
    return this.group;
    }

    public void setGroup(com.ibm.cruser.model.Group group) {
    this.group = group;
    }

    /**
    * @hibernate.many-to-one
    * update="false"
    * insert="false"
    *
    * @hibernate.column
    * name="userid"
    *
    */
    public com.ibm.cruser.model.User getUser() {
    return this.user;
    }

    public void setUser(com.ibm.cruser.model.User user) {
    this.user = user;
    }

    public String toString() {
    return new ToStringBuilder(this)
    .append("comp_id", getComp_id())
    .toString();
    }

    public boolean equals(Object other) {
    if ( !(other instanceof Member) ) return false;
    Member castOther = (Member) other;
    return new EqualsBuilder()
    .append(this.getComp_id(), castOther.getComp_id())
    .isEquals();
    }

    public int hashCode() {
    return new HashCodeBuilder()
    .append(getComp_id())
    .toHashCode();
    }

    }

    public class MemberPK implements Serializable {

    /** identifier field */
    private Integer userid;

    /** identifier field */
    private Integer groupid;

    /** full constructor */
    public MemberPK(Integer userid, Integer groupid) {
    this.userid = userid;
    this.groupid = groupid;
    }

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

    /**
    * @hibernate.property
    * column="userid"
    * length="4"
    *
    */
    public Integer getUserid() {
    return this.userid;
    }

    public void setUserid(Integer userid) {
    this.userid = userid;
    }

    /**
    * @hibernate.property
    * column="groupid"
    * length="4"
    *
    */
    public Integer getGroupid() {
    return this.groupid;
    }

    public void setGroupid(Integer groupid) {
    this.groupid = groupid;
    }

    public String toString() {
    return new ToStringBuilder(this)
    .append("userid", getUserid())
    .append("groupid", getGroupid())
    .toString();
    }

    public boolean equals(Object other) {
    if ( !(other instanceof MemberPK) ) return false;
    MemberPK castOther = (MemberPK) other;
    return new EqualsBuilder()
    .append(this.getUserid(), castOther.getUserid())
    .append(this.getGroupid(), castOther.getGroupid())
    .isEquals();
    }

    public int hashCode() {
    return new HashCodeBuilder()
    .append(getUserid())
    .append(getGroupid())
    .toHashCode();
    }

    }

  4. #4
    Join Date
    Sep 2004
    Posts
    15

    Default

    Hi theone,

    i have the same problem. My code is very similiar:

    http://forum.springframework.org/showthread.php?t=10328

    In my case this only works when i use OpenSessionInView filter. Did you try that? What i don't understand is that at the point when i call save/update the view is far from being involved. I don't get it. But try OpenSessionInView filter if you're running a webapp and check if it works then.

    -andi
    Last edited by Rod Johnson; Jan 18th, 2006 at 10:58 AM.

Similar Threads

  1. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  2. Replies: 13
    Last Post: Dec 7th, 2004, 10:00 AM
  3. Replies: 6
    Last Post: Nov 24th, 2004, 10:06 AM
  4. Replies: 8
    Last Post: Sep 23rd, 2004, 01:12 AM
  5. Replies: 2
    Last Post: Aug 17th, 2004, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •