-->
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.  [ 3 posts ] 
Author Message
 Post subject: can't save associated tables
PostPosted: Wed Jul 06, 2005 3:08 pm 
Newbie

Joined: Mon Jun 13, 2005 9:29 am
Posts: 8
Hi,

maybe it was asked before, but I can't find it.

My problem is, that I have 2 tables which are linked by a foreign key. Nothing special on it. Both tables have auto_increment enabled. The mapping files are generated by Middlegen (MiddlegenIDE and MyEclipse 3.0). But when I populate my model objects and try to save it, I get an error that the foreign key column is null. I thought that Hibernate will populate the value automatically ?

I'm using MySQL 4 and Hibernate 3.
Here are the mapping files:

-- table calendar --
<class
name="model.Calendar"
table="calendar"
>

<id
name="id"
type="java.lang.Integer"
column="id"
>

<generator class="native" />
</id>
<!-- Associations -->

<!-- bi-directional one-to-many association to Event -->
<set
name="events"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="calendar_id" />
</key>
<one-to-many
class="model.Event"
/>
</set>

-- table event (with the foreign to calendar)--
<class
name="model.Event"
table="event"
>

<id
name="id"
type="java.lang.Integer"
column="id"
>

<generator class="native" />
</id>
<!-- Associations -->

<!-- bi-directional many-to-one association to Calendar -->
<many-to-one
name="calendar"
class="model.Calendar"
not-null="true"
>
<column name="calendar_id" />
</many-to-one>

-- hibernate output --
DEBUG - could not insert: [model.Event] [insert into event ...
java.sql.SQLException: Column 'calendar_id' cannot be null

-- class CalendarService.java --

public void save (Calendar calendar) {
HibernateUtil.getSession().save(calendar) ;
}

Thanks for any help
Lothar


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 8:58 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 8:18 pm
Posts: 23
How did you add Event to Calendar? Something like this?

void add(Event evt) {
events.add(evt);
evt.setCalendar(this);
}

Check the docs on bi-directional mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 6:58 am 
Newbie

Joined: Mon Jun 13, 2005 9:29 am
Posts: 8
Thanks for your help.

But I can't get it working. I found an error in my database design so that I just have a simple onte-to-many relation. So now all seems to be ok, no errors, but the values aren't saved in the database.

What I'm doing wrong ? I've red the docu and some samples, but can't find the mistake.

Here the corrected mapping files:
-- class calendar --

<class
name="CalendarRO"
table="calendar"
>
<id
name="Id"
type="integer"
column="id"
unsaved-value="null"
>
<generator class="identity"/>
</id>

<set name="events" inverse="true">
<key column="id"/>
<one-to-many class="EventRO"/>
</set>
</class>

-- class Event--
<class
name="EventRO"
table="event"
>
<id
name="Id"
type="integer"
column="id"
unsaved-value="null"
>
<generator class="assigned"/>
</id>
<many-to-one
name="calendar"
column="calendar_id"
class="CalendarRO"
not-null="true"
>
</many-to-one>


My java code looks like:

CalendarRO calendarRO = new CalendarRO() ;
//calendarRO.setEvents(eventSet);
calendarRO.setEvents(new HashSet()) ;

for (getEvents().iterator) {

EventRO eventRO = new EventRO();
eventRO.setCalendar(calendarRO);
//eventSet.add(eventRO);
calendarRO.getEvents().add(eventRO) ;


}

HibernateUtil.getSession().saveOrUpdate(calendarRO)


It would be very nice if you can put me to the right direction.
Thanks, Lothar


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