File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ram Raju
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic