-->
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.  [ 5 posts ] 
Author Message
 Post subject: Parent/Child Problem - NonUniqueObjectException
PostPosted: Thu Jun 17, 2004 10:07 am 
Newbie

Joined: Thu May 13, 2004 1:37 pm
Posts: 9
OK, I'm quite new to Hibernate and have read the Parent/Child Example section (more than twice!) and just have myself stuck. I think I'm struggling with terminology a bit and therefore getting confused.

I know this smells of newbie, RTFM. I am and I have! Any help is still greatly appreciated.

Hibernate 2.1.4 Tomcat 5.0.19 and MS SQL Server 2000. I'm able to "Hibernate" one-off POJO's so I think my environment is OK.

I'm trying to create a simple 1-N relationship between two tables. In a roundabout way it's defining something similar to an HTML <SELECT> and all it's <OPTION> entries.

So, I have two tables:

optionlists -> id INT (PK), name VARCHAR.
optionlist_entries -> id INT (PK), optionlist_id INT, value VARCHAR.

Simple enough. I then create two POJO's for these tables. They do have public getters and setters.
Code:
public class OptionList {
private int id;
private String name;
private Set entries;
}

public class OptionListEntry {
  private int id;
  private int optionlist_id;
  private String value;
  OptionList optionList;
}

Still very simple. So I created two mapping files --

Code:
== OptionList.hbm.xml ==
<hibernate-mapping>
  <class name="x.y.OptionList" table="optionlists>
        <id name="id" type="int" unsaved-value="null" >
            <column name="id" sql-type="integer" not-null="true"/>
            <generator class="identity" />
        </id>
        <property name="name" />
       
        <set name="entries" inverse="true">
          <key column="optionlist_id" />
          <one-to-many class="x.y.OptionListEntry" />
        </set>
  </class>
</hibernate-mapping>

== OptionListEntry.hbm.xml ==
<hibernate-mapping>
  <class name="x.y.OptionListEntry" table="optionlist_entries">
    <id name="id" type="int" unsaved-value="null" >
      <column name="id" sql-type="integer" not-null="true"/>
      <generator class="identity" />
    </id>
    <property name="value" />
    <many-to-one name="optionList" column="optionlist_id" not-null="true" />
    </class>
</hibernate-mapping>


However, given the following code --

Code:
  Transaction tx = session.beginTransaction();
         
  OptionList ol = new OptionList();
  ol.setName("US National team");
         
  Set entries = new HashSet();
 
  OptionListEntry ole = new OptionListEntry();
  ole.setValue("Friedel, Brad");
  entries.add(ole);
         
  ol.setEntries(entries);
         
  session.save(ol);
         
  tx.commit();


-- session.save(ol); produces the following console entry and exception.

Code:
Hibernate: insert into optionlists (name) values (?)

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: com.wrgd.dd.OptionListEntry
   at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1673)
   at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1442)
   at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1469)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1392)
   at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
   at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)


I'm at a loss here. Anyone?

Thanks!


Top
 Profile  
 
 Post subject: Re: Parent/Child Problem - NonUniqueObjectException
PostPosted: Thu Jun 17, 2004 10:54 am 
Newbie

Joined: Thu May 13, 2004 1:37 pm
Posts: 9
I should note that the following code
Code:
List optionLists = session.find("from OptionList");

produces exactly the results I would expect. I get a collection of OptionList objects. Each OptionList.entries Set contains some number of OptionListEntries.


Top
 Profile  
 
 Post subject: Re: Parent/Child Problem - NonUniqueObjectException
PostPosted: Thu Jun 17, 2004 1:19 pm 
Newbie

Joined: Thu May 13, 2004 1:37 pm
Posts: 9
Let me follow this up again with some information about what has me confused.

Referring to Chapter 16: Example Parent/Child of the reference. I'm essentially doing exactly as described - I think. My "parent" is OptionList and my "child" OptionListEntry.

The first thing that I may be misunderstanding is the <set ...> example in section 16.2. In particular, I believe the parent_id column is referring to a column in the child table as opposed to the primary key on the parent table. That seems obvious. Is my assumption correct?

Likewise, a bit further in 16.2 there's the <many-to-one ...> example to be placed in the child's .hbm.xml. I assume this column also refers to the child table.

The other thing that I'm not understanding is whether or not the child class is
required to have a reference to the parent class or if it's just optional. In any case, I still have a problem either way. However, for this particular application I have no need to navigate from the child to the parent so I'd prefer to leave it off unless Hibernate requires it.

The other thing that I'd find helpful would be having the full example described in Chapter 16 available. POJO's, servlet/client code, DDL, full Hibernate .xml's, etc. I'm just having trouble tying it all together when I'm seeing only snippets (or nothing) from these items.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 2:34 pm 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
Well, I'm a newbie. But I guess that you have to set cascade if you want to propagate the save from OpEntry to OpList. If I'm wrong please return me :)
Also I think that something may be wrong with the PK. It's int and you are using unsaved-value="null". I guess that it is including a zero always, getting repeated items, becouse int is never null (Integer may be null)
hope it helps


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 2:56 pm 
Newbie

Joined: Thu May 13, 2004 1:37 pm
Posts: 9
Well, atorres. Your three newbie posts certainly outweigh mine!! The unsaved-value="null" was exactly the problem. Apparently I'm in the habit lately of misunderstanding perfectly clear documentation.

Thanks for correcting me!!


atorres wrote:
Well, I'm a newbie. But I guess that you have to set cascade if you want to propagate the save from OpEntry to OpList. If I'm wrong please return me :)
Also I think that something may be wrong with the PK. It's int and you are using unsaved-value="null". I guess that it is including a zero always, getting repeated items, becouse int is never null (Integer may be null)
hope it helps


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.