-->
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.  [ 2 posts ] 
Author Message
 Post subject: help in modelling an many-to-many relationship
PostPosted: Thu Mar 01, 2007 9:56 pm 
Newbie

Joined: Thu Mar 01, 2007 9:39 pm
Posts: 1
Dear hibernate users:
I am trying to model a many-to-many relationship in hibernate as a one-to-many relationship between first entity and intersection entity and as a many-to-one between intersection entity and second entity. Both foreign keys in intersection table are NOT NULL. The mapping is shown below. When I run the java code snippet below, I get a NOT NULL insert exception.

The problem is that hibernate tries to insert the record in the intersection table first with NULL for the foreign key to the first entity(one-part) of the relationship, and then update the foreign key in the second statement. Inserting a NULL into a NOT NULL column in the intersection table will throw the exception. What can I do to tell hibernate to do one insert instead of an insert followed by an update?


Hibernate version:
3.2

Mapping documents:
<hibernate-mapping>
<class name="hello.Event" table="events">
<id name="id" column="uid" type="long">
<generator class="native"></generator></id>
<property name="name" type="string"/>
<property name="startDate" column="start_date" type="date"/>
<property name="duration" type="integer"/>

<set name="eventspeakers" cascade="all">
<key column="event_id" />
<one-to-many class="hello.EventSpeaker" />
</set>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="hello.Speaker" table="speakers">
<id name="id" column="uid" type="long">
<generator class="native"></generator></id>
<property name="name" type="string"/>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="hello.EventSpeaker" table="events_speakers ">
<id name="id" column="uid" type="long">
<generator class="native"></generator></id>
<property name="eventId" type="long" not-null="true" column="EVENT_ID" insert="false" update="false"/>
<property name="speakerId" type="long" column="SPEAKER_ID" />
<property name="owner" type="string"/>
<many-to-one cascade="none" class="hello.Speaker" name="speaker" not-null="true" insert="false" update="false" column="SPEAKER_ID" not-found="exception" embed-xml="true"/>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Event event = (Event)session.load(Event.class, new Long(1));


Speaker speaker = (Speaker)session.load(Speaker.class, new Long(1));

EventSpeaker es = new EventSpeaker(speaker);

event.getSpeakers().add(es);

session.saveOrUpdate(event);

The generated SQL (show_sql=true):
insert into events_speakers (SPEAKER_ID, owner) values (?, ?)
update events_speakers set event_id=? where uid=?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 5:44 am 
Newbie

Joined: Fri Nov 03, 2006 5:21 am
Posts: 14
Hi,

the modeling of your intermediate EventSpeaker class is really strange.
Fist of all, you have a property speakerID and a association to speaker. What do you need the property for?

But the real problem is, that you have an eventId that is never set.

Why do you not modle your EventSpeaker like this:

Code:
<hibernate-mapping>
    <class name="cEventSpeaker" table="EVENT_SPEAKER">
         <id name="id" column="uid" type="long">
       
        <property name="owner" type="string"/>

        <many-to-one name="event"
           column="EVENTID_ID"
           class="Event"
           not-null="true"
           cascade="none"
        />
       
        <many-to-one name="speaker"
           column="SPEAKER_ID"
           class="Speaker"
           not-null="true"
           cascade="none"
        />

    </class>
   
</hibernate-mapping>


And when you create the association you do something like this:

Code:
Event event = (Event)session.load(Event.class, new Long(1));
Speaker speaker = (Speaker)session.load(Speaker.class, new Long(1));
EventSpeaker eventSpeaker = new EventSpeaker();
eventSpeaker.setEvent(event);
eventSpeaker.setSpeaker(speaker);
session.saveOrUpdate(eventSpeaker);


Bye the way, the problem in your code most likely comes from your call
event.getSpeakers().add(es); During this call Hibernate has to load the lazy association from event to EventSpeaker and has to write all the changes made before into the DB. And at that moment EventSpeaker has no event set...

Regards,
Joern


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