This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate: one-to-many bidirectional mapping. Stack Overflow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: one-to-many bidirectional mapping. Stack Overflow" Watch "Hibernate: one-to-many bidirectional mapping. Stack Overflow" New topic
Author

Hibernate: one-to-many bidirectional mapping. Stack Overflow

Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
Hi,

I am trying a one-to-many bidirectional mapping.
An Event has multiple EventAttributes.

Event event = new Event();

Set s = new HashSet();
EventAttribute ea = new EventAttribute();
ea.setEvent(event);
s.add(ea);

event.setEventAttributes(s);

dao.saveEvent(event);

Mapping:

<class name="Event" table="event">
<id name="eventId" column="event_id" unsaved-value="0">
<generator class="increment"/>
</id>
<set name="eventAttributes" table="event_attribute" lazy="false" cascade="all" inverse="true">
<key column="event_id" />
<one-to-many class="EventAttribute"/>
</set>
</class>
<class name="EventAttribute" table="event_attribute">
<id name="eventAttributeId" column="event_attribute_id" unsaved-value="0">
<generator class="increment"/>
</id>
<many-to-one name="event" column="event_id" class="Event" not-null="true"/>
</class>

This causes a stack overflow error.!!

I am stuck. Please help.

[ March 14, 2005: Message edited by: Ram Raju ]
[ March 14, 2005: Message edited by: Ram Raju ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Where does the stack overflow occur? Can you post the stack trace and point out where it's failing in your code?

Also, can you post your EventDAO code so we can see what calls you're making to Hibernate?
[ March 14, 2005: Message edited by: David Harkness ]
Clemens Tolboom
Greenhorn

Joined: Feb 15, 2005
Posts: 12
Why are you using
- s.add(ea);
in
- Event event = new Event();

- Set s = new HashSet();
- EventAttribute ea = new EventAttribute();
- ea.setEvent(event);
- s.add(ea);
?

Hibernate should take care about the inverse relation.
Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
David:

The java.lang.StackOverflowError occurs in JUnit test.

The method in EventDAO impl.


Clemens:
I am adding the EventAttribute to set and adding the set to event. I am also setting the event in the EventAttribute.



I am adding the EventAttribute to set and also setting the Event in the event attribute. Is this circular assignment causing the overflow.

If I dont do
- s.add(ea);
the event is created, but the attributes table is empty.

[ March 15, 2005: Message edited by: Ram Raju ]
[ March 15, 2005: Message edited by: Ram Raju ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Ram Raju:
I'd definitely check your use of HashCodeBuilder. The HashSet uses hashCode() which is causing the exception.
Ram Raju
Greenhorn

Joined: Mar 11, 2003
Posts: 17
Ok. I found what was causing Stack overflow.

I started development using AppFuse and was extending BaseObject in all my model objects. For those who havent looked at appfuse, BaseObject is just a Serializable bean which overrides toString, equals and hashCode methods.

When I extended BaseObject in both parent and child and tried to set one in the other, something in the hashCode method did not like it. When I stopped extending BaseObject in child, everything was ok.

Thanks David & Clemens for helping me out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: one-to-many bidirectional mapping. Stack Overflow
 
Similar Threads
update while doing save in hibernate
Indexed Collections
Mapping Exception
Lazy loading not working as expected
hibernate - insert object failed because of foreing key violation