-->
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: Why I have to add reciprocally 2 object for correct persiste
PostPosted: Thu Mar 22, 2012 7:57 am 
Newbie

Joined: Wed Jan 12, 2011 1:15 pm
Posts: 11
I have add "A in B" and "B in A" for hibernate save correctly their FK in database, for example in this code:

Code:
miembro.setAsociacion(asociacion);
asociacion.setMiembros(miembros);

asociacionBo.save(asociacion)
;


the result is :

- One row in database representing association
- One row in database representing member (with FK association add)

In this code:

Code:
//miembro.setAsociacion(asociacion);
asociacion.setMiembros(miembros);
asociacionBo.save(asociacion);


the result is:
- One row in database representing association
- One row in database representing member (without FK association add)


this is associationĀ“s mapping:

Code:
@OneToMany(mappedBy="asociacion",cascade=CascadeType.ALL,fetch=FetchType.EAGER,orphanRemoval=true)
private Set<Miembro> miembros;


this is memberĀ“s mapping:

Code:
@ManyToOne
@JoinColumn(name="IDASOCIACION",referencedColumnName="IDASOCIACION")
private Asociacion asociacion;


I thought that pesistence since Asociation was in cascade and when hibernate insert asociation row,hibernate insert row member with FK asociation but it dont work in this way.

I dont understand why.

somebody can explain this behavior?

Kind regards.


Top
 Profile  
 
 Post subject: Re: Why I have to add reciprocally 2 object for correct persiste
PostPosted: Thu Mar 22, 2012 8:51 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi Parche,

the reason is, and it's well documented, that on bidirectional relation you always must set both sides of the relation.
Code:
miembro.setAsociacion(asociacion); // this is the owner side, if you comment out this, then the relationship get not persisted (->member without FK association)
asociacion.setMiembros(miembros); // this is the inverse side, if you comment out this, then the relationship get persisted anyway,
but in your current persistent context the miembros set is in a wrong state.


Top
 Profile  
 
 Post subject: Re: Why I have to add reciprocally 2 object for correct persiste
PostPosted: Thu Mar 22, 2012 10:53 am 
Newbie

Joined: Wed Jan 12, 2011 1:15 pm
Posts: 11
Thank you very much. I didnt see this information.


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.