-->
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.  [ 1 post ] 
Author Message
 Post subject: one-to-many update problem
PostPosted: Tue Jul 24, 2007 8:57 am 
Newbie

Joined: Wed Jul 11, 2007 6:06 am
Posts: 15
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


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

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.