Hi,
My application involves 4 classes: Person, Task, Place, and Timeline.
Their relationships are as follows:
1. one Person may have many Task (one-to-many).
2. one Person may have many Place (one-to-many).
3. one Task may perform on many Timeline and in different Place. That
means one Task may have many Timeline, and one Place may be
used to perform many Timeline.
So I created the following mapping files:
in Person.hbm.xml
...
<set name="tasks" cascade="all">
<key column="tadk_personid"/>
<one-to-many class="Task"/>
</set>
<set name="places" cascade="all">
<key column="place_personid"/>
<one-to-many class="Place"/>
</set>
...
in Task.hbm.xml
...
<many-to-one name="person" class="Person" column="task_personid" cascade="all"/>
<set name="timelines" cascade="all">
<key column="timeline_taskid"/>
<one-to-many class="Timeline"/>
</set>
...
in Place.hbm.xml
...
<many-to-one name="person" class="Person" column="place_personid" cascade="all"/>
...
in Timeline.hbm.xml
...
<property name="startDate" type="java.util.Date" column="timeline_startdate"/>
<many-to-one name="task" class="Task" column="timeline_taskid" cascade="all"/>
<many-to-one name="place" class="Place" column="timeline="placeid" cascade="all"/>
...
I don't want to get a set of Timelines from a Place, so there is no one-to-many set in the Place.hbm.xml.
The propblem is when I try to save a new Timeline, I always got the NonUniqueObjectException. :(
my codes are as follows. (I use the HibernateHelper and DAO to do
the hibernate operation.):
...
Timeline newTime = new TimeLine();
newTime.setStartDate(new Date());
newTime.setTask(DAOFactory.getTaskDAO().getTask(taskId));
newTime.setPlace(DAOFactory.getPlaceDAO().getPlace(placeId));
DAOFactory.getTimelineDAO().saveOrUpdate(newTime);
....
Hibernate version: 2.1.6
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: Task
Name and version of the database you are using:
MySQL 4.0.18
I spent weeks in this issue! I am so frustrated! :(
Any suggestion is appratiated.
Thanks.
Sean
|