Hi everybody,
Hibernate version: 3.2.1
Mapping documents:
I have three classes: Event, EventInstance and NewsItem. An Event can have multiple EventInstances, however each EventInstance belongs to one Event. Each EventInstance also belongs to one NewsItem, and one NewsItem can have multiple EventInstances. Besides that each EventInstance has two String variables.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="nl.semlab.viewerpro.client.core.event">
<class name="DefaultEvent" table="event">
<id name="id" type="long" unsaved-value="-1">
<generator class="native" />
</id>
<property name="name" type="string" length="100" not-null="true" />
<property name="description" type="string" />
<!-- TODO: figure out why we have to use lazy="false" here -->
<set name="eventActions" table="eventAction" lazy="false" cascade="all">
<key column="eventId" />
<one-to-many class="nl.semlab.viewerpro.client.core.eventaction.DefaultEventAction" />
</set>
<!-- TODO: figure out why we have to use lazy="false" here -->
<set name="eventInstances" table="eventInstance" lazy="false" cascade="all">
<key column="eventId" />
<one-to-many class="nl.semlab.viewerpro.client.core.eventinstance.DefaultEventInstance" />
</set>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="nl.semlab.viewerpro.client.core.event">
<class name="DefaultEvent" table="event">
<id name="id" type="long" unsaved-value="-1">
<generator class="native" />
</id>
<property name="name" type="string" length="100" not-null="true" />
<property name="description" type="string" />
<!-- TODO: figure out why we have to use lazy="false" here -->
<set name="eventActions" table="eventAction" lazy="false" cascade="all">
<key column="eventId" />
<one-to-many class="nl.semlab.viewerpro.client.core.eventaction.DefaultEventAction" />
</set>
<!-- TODO: figure out why we have to use lazy="false" here -->
<set name="eventInstances" table="eventInstance" lazy="false" cascade="all">
<key column="eventId" />
<one-to-many class="nl.semlab.viewerpro.client.core.eventinstance.DefaultEventInstance" />
</set>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="nl.semlab.viewerpro.client.core.event">
<class name="DefaultEvent" table="event">
<id name="id" type="long" unsaved-value="-1">
<generator class="native" />
</id>
<property name="name" type="string" length="100" not-null="true" />
<property name="description" type="string" />
<!-- TODO: figure out why we have to use lazy="false" here -->
<set name="eventActions" table="eventAction" lazy="false" cascade="all">
<key column="eventId" />
<one-to-many class="nl.semlab.viewerpro.client.core.eventaction.DefaultEventAction" />
</set>
<!-- TODO: figure out why we have to use lazy="false" here -->
<set name="eventInstances" table="eventInstance" lazy="false" cascade="all">
<key column="eventId" />
<one-to-many class="nl.semlab.viewerpro.client.core.eventinstance.DefaultEventInstance" />
</set>
</class>
</hibernate-mapping>
Name and version of the database you are using: HSQLDB-1.8.0.7
I am trying to modfiy an EventInstance, at this point is is associated with newsitem 67 and event 1. I want to associate it with another event (2), the association with the newsitem is fine.
This is basically what I am doing:
Code:
// Remove the event instance from the old event.
Set<EventInstance> instances = eventInstance.getEvent().getEventInstances();
boolean remove = instances.remove(eventInstance);
eventInstance.getEvent().setEventInstances(instances);
eventInstance.setEvent(event);
// Add the event instance to the new event.
event.getEventInstances().add(eventInstance);
Although the "remove" boolean is "true" when I print it, the EventInstance keeps coming back to the old event when I save everything when closing my application. During this save process I printed everything that happened. To my surprise the EventInstance is in the EventInstance Set of both Event 1 and Event 2.
Does somebody know what I am doing wrong?
--
Best regards,
Jethro Borsje