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!