-->
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.  [ 7 posts ] 
Author Message
 Post subject: Confused in one-to-many mapping with <list>
PostPosted: Thu Apr 24, 2008 10:17 pm 
Newbie

Joined: Wed Nov 15, 2006 8:51 am
Posts: 12
Hi all,

I have some trouble in mapping with a <list> in a <one-to-many> condition

I have 2 tables:
lm_group and lm_access_rule

lm_group will have a List of lm_access_rule objects (ie. one-to-many)
I have tried to config the tables in the hbm.xml for several options but still failed to save/add lm_access_rule objects.

common errors I have are the following:
1) org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session
2) Column 'idx' cannot be null
3) uncategorized SQLException for SQL [insert into lm_access_rule (groupId, rule) values (?, ?)]; SQL state [HY000]; error code [1364]; Field 'idx' doesn't have a default value; nested exception is java.sql.SQLException: Field 'idx' doesn't have a default value

Attached is the table scripts (mysql) + the hbm.xml for your reference
PS. for sure... hibernate didn't handle the "idx" field when trying to persist a lm_access_rule object to the database and therefore throw the above exception(s)

[Scripts]
CREATE TABLE `lm_group` (
`id` int(10) unsigned NOT NULL auto_increment COMMENT 'id / pk for lm_group',
`groupName` varchar(100) NOT NULL COMMENT 'group name',
`description` varchar(200) default NULL COMMENT 'group description',
`status` varchar(1) NOT NULL default 'A',
PRIMARY KEY (`id`),
UNIQUE KEY `unique_groupName` (`groupName`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

CREATE TABLE `lm_access_rule` (
`groupId` int(10) unsigned NOT NULL,
`rule` varchar(200) character set big5 NOT NULL,
`idx` int(10) unsigned NOT NULL,
UNIQUE KEY `unique_group_rule` USING BTREE (`groupId`,`rule`,`idx`),
CONSTRAINT `FK_lm_access_rule_1` FOREIGN KEY (`groupId`) REFERENCES `lm_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

[hbm.xml]

<class
name="BasicCredentialGroup"
table="lm_group">
<id name="id" type="int">
<generator class="native" />
</id>
<property name="groupName" type="string" length="100" />
<property name="description" type="string" length="200" />
<property name="status" type="string" length="1" />

<!-- a 1-to-many mapping -->
<list name="rules" table="lm_access_rule" >
<key column="groupId" />
<list-index column="idx" />
<one-to-many
class="BasicCredentialAccessRule" />
</list>
</class>

<class
name="BasicCredentialAccessRule"
table="lm_access_rule">
<composite-id>
<key-property name="groupId" type="int" />
<key-property name="idx" type="int" />
</composite-id>
<property name="rule" type="string" length="200" />
</class>


Any help or hints would be highly appreciated, thanks

Jason (kusanagihk)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 6:39 am 
Newbie

Joined: Fri Apr 25, 2008 5:09 am
Posts: 4
Hi,

Regarding your first error

1) org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session

this error occurs if you try to attach an object to the persistence context with an identifier that is alread attached. I had this case several times recently. Check your code if the object you are trying do persist ist loaded earlier. This can happen without loading it explicitly by traversing the object graph, where the object may be included.

cheers
elch


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 10:35 am 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
Hi Try using not-null = "true" in the key field of the list

i.e <key column="" notnull="true"/>

I guess this should work let us know how u solved the problem thanks


Top
 Profile  
 
 Post subject: The explanations
PostPosted: Mon Apr 28, 2008 4:18 am 
Newbie

Joined: Wed Nov 15, 2006 8:51 am
Posts: 12
Yep it is obvious becoz hibernate didn't handle the list index's column; therefore the value must be null for every lm_access_rule instance; and therefore the "identifier" is "already attached" (assume the first null value is acceptable, but the 2nd null value means a duplicate)

So the problem goes back to ... why hibernate couldn't increment the "idx" field.


elch78 wrote:
Hi,

Regarding your first error

1) org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session

this error occurs if you try to attach an object to the persistence context with an identifier that is alread attached. I had this case several times recently. Check your code if the object you are trying do persist ist loaded earlier. This can happen without loading it explicitly by traversing the object graph, where the object may be included.

cheers
elch


Top
 Profile  
 
 Post subject: Still don't work
PostPosted: Mon Apr 28, 2008 4:34 am 
Newbie

Joined: Wed Nov 15, 2006 8:51 am
Posts: 12
Um I have tried the "not-null" option; but similar errors giving the same problem again --> hibernate failed to increment the "idx" field

Code:
There are 2 variations I have tried on the "not-null" case:
[option 1]
    <class
        name="BasicCredentialGroup"
        table="lm_group">
        <id name="id" type="int">
            <generator class="native" />
        </id>
        <property name="groupName" type="string" length="100" />
        <property name="description" type="string" length="200" />
        <property name="status" type="string" length="1" />
       
        <!-- a 1-to-many mapping -->
        <list name="rules" table="lm_access_rule" >
            <key column="groupId" not-null="true" />
            <index column="idx" />
            <one-to-many
                class="BasicCredentialAccessRule" />
        </list>
    </class>

    <class
        name="BasicCredentialAccessRule"
        table="lm_access_rule">
        <id name="id" type="int">
            <generator class="native" />
        </id>
        <property name="rule" type="string" length="200" />
    </class>

Exceptions found:
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value.

[option 2]
    <class
        name="BasicCredentialGroup"
        table="lm_group">
        <id name="id" type="int">
            <generator class="native" />
        </id>
        <property name="groupName" type="string" length="100" />
        <property name="description" type="string" length="200" />
        <property name="status" type="string" length="1" />
       
        <!-- a 1-to-many mapping -->
        <list name="rules" table="lm_access_rule" >
            <key column="groupId" />
            <index column="idx" />
            <one-to-many
                class="BasicCredentialAccessRule" />
        </list>
    </class>

    <class
        name="BasicCredentialAccessRule"
        table="lm_access_rule">
        <id name="id" type="int">
            <generator class="native" />
            </id>
                <property name="groupId" type="int" not-null="true" />
                <property name="idx" type="int" not-null="true" />
        <property name="rule" type="string" length="200" />
    </class>

Exception found:
Cannot insert null value to BasicCredentialAccessRule.idx


Sort of confusing....



sainaveenp wrote:
Hi Try using not-null = "true" in the key field of the list

i.e <key column="" notnull="true"/>

I guess this should work let us know how u solved the problem thanks
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 28, 2008 1:59 pm 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
Can you provide the class where you are trying to set the values for the classes and how you are saving the values..also try removing the not-null=true you have in the BasicCredentials class i.e for

<property name="idx" not-null="true"> remove that


Top
 Profile  
 
 Post subject: Code
PostPosted: Mon Apr 28, 2008 9:48 pm 
Newbie

Joined: Wed Nov 15, 2006 8:51 am
Posts: 12
The code for the classes are below:

Code:
public class BasicCredentialGroup implements Serializable
{
   static final long serialVersionUID=1l;
   
   Integer id;
   String groupName;
   String description;
   String status;
   List rules;
      
   /**
    * @return the id
    */
   public Integer getId()
   {
      return id;
   }
   /**
    * @param id the id to set
    */
   public void setId(Integer id)
   {
      this.id = id;
   }
   /**
    * @return the groupName
    */
   public String getGroupName()
   {
      return groupName;
   }
   /**
    * @param groupName the groupName to set
    */
   public void setGroupName(String groupName)
   {
      this.groupName = groupName;
   }
   /**
    * @return the description
    */
   public String getDescription()
   {
      return description;
   }
   /**
    * @param description the description to set
    */
   public void setDescription(String description)
   {
      this.description = description;
   }
   /**
    * @return the rules
    */
   public List getRules()
   {
      return rules;
   }
   /**
    * @param rules the rules to set
    */
   public void setRules(List rules)
   {
      this.rules = rules;
   }
   /**
    * @return the status
    */
   public String getStatus()
   {
      return status;
   }
   /**
    * @param status the status to set
    */
   public void setStatus(String status)
   {
      this.status = status;
   }
}

public class BasicCredentialAccessRule implements Serializable
{
   static final long serialVersionUID = 1l;

   Integer id;
   Integer groupId;
   String rule;

   /**
    * @return the id
    */
   public Integer getId()
   {
      return id;
   }

   /**
    * @param id
    *        the id to set
    */
   public void setId(Integer id)
   {
      this.id = id;
   }

   /**
    * @return the groupId
    */
   public Integer getGroupId()
   {
      return groupId;
   }

   /**
    * @param groupId
    *        the groupId to set
    */
   public void setGroupId(Integer groupId)
   {
      this.groupId = groupId;
   }

   /**
    * @return the rule
    */
   public String getRule()
   {
      return rule;
   }

   /**
    * @param rule
    *        the rule to set
    */
   public void setRule(String rule)
   {
      this.rule = rule;
   }
}



I think the classes are just the simplest POJO; therefore, it should be the settings in the hbm.xml I have missed out.


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

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.