• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving associated objects in hibernate one-to-one mapping

 
swetha ma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one-to-one relationship between two objects (EventActivity & Event).

public class EventActivity extends AbstractTrainingActivity
{

@OneToOne(fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn
private Event event;

public Event getEvent() {
return event;
}

public void setEvent(Event event) {
this.event = event;
}

}

when I try to save EventActivity, everything in AbstractTrainingActivity is getting saved but the Event Object is not getting saved.

Is there any way to tell hibernate to save the associated objects in one-to-one mapping ???
 
Shannon McGee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swetha,

You should add the "cascade" attribute to the OneToOne annotation.

Your annotation will then look like:
@OneToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)

Adding the cascade annotation will persist, update, or delete the aggregated event when you take that action on the EventActivity.

-Shannon
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic