-->
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.  [ 12 posts ] 
Author Message
 Post subject: Deleting Child with other updates
PostPosted: Mon Jan 26, 2009 12:23 pm 
Newbie

Joined: Mon Jan 26, 2009 12:14 pm
Posts: 8
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version:3.0

[b]Mapping documents:
PARENT
<!-- bi-directional one-to-many association to AgreementProperty -->
<set name="AgreementProperties" lazy="false" inverse="false" cascade="all-delete-orphan">
<key>
<column name="AGRMT_ID" />
</key>
<one-to-many class="com.mutualofomaha.iaa.bean.agreement.AgreementProperty" />
</set>

CHILD
<!-- bi-directional many-to-one association to Agreement -->
<many-to-one name="Agreement" foreign-key="AGRMT_ID" class="com.mutualofomaha.iaa.bean.agreement.Agreement" not-null="true" insert="true" update="true">
<column name="AGRMT_ID" />
</many-to-one>


Hello Everyone, this maybe a newb question but I am frustrated.

I am trying to simply delete a child. My mappings are above.

Basically we open a session, grab the object, overlay values, remove possible objects(like removing a child) then do a saveOrUpdate and then loop through all deletes then session.flush(); Every combination I have tried I get errors with.

Should I be doing the deletes and then flush and then saveorUpdate then flush()?

So I have tried to remove from parent set and delete through session. NullPointer within some transientEntity class.

Next I removed the delete on session of the child and just removed from parent set. Same thing.

I have been searching on internet for any information and just keep getting more frustrated. My example there is no intermediate join table. I simply want hibernate to delete the child row.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 12:41 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You could try the following:
1.
Code:
<key>
<column name="AGRMT_ID" />
</key>
should be
Code:
<key column="AGRMT_ID"/>


2. As you have mapped AgreementProperties as inverse="false",
Code:
<many-to-one name="Agreement" foreign-key="AGRMT_ID" class="com.mutualofomaha.iaa.bean.agreement.Agreement" not-null="true" insert="true" update="true">
should have update ="false", as the collection side is the owner. Otherwise just map your collection as inverse="true".

3. You don't need to explicitly delete the childs when using delete-orphan. Hibernate detects entities that were removed from the owning collection and deletes them. Just remove these out of the collection and flush the session (maybe you have to call merge in your parent object, if it is detached).

Hope that helps.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 12:55 pm 
Newbie

Joined: Mon Jan 26, 2009 12:14 pm
Posts: 8
Ok tried that. Getting nullpointer exception.

java.lang.NullPointerException
at org.hibernate.event.def.DefaultDeleteEventListener.deleteTransientEntity(DefaultDeleteEventListener.java:183)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:81)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:775)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:758)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:121)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)

Now here is my current config
PARENT
<!-- bi-directional one-to-many association to AgreementProperty -->
<set name="AgreementProperties" lazy="false" inverse="true" cascade="all-delete-orphan">
<key column="AGRMT_ID"/>
<one-to-many class="com.mutualofomaha.iaa.bean.agreement.AgreementProperty" />
</set>
CHILD
<!-- bi-directional many-to-one association to Agreement -->
<many-to-one name="Agreement" foreign-key="AGRMT_ID" class="com.mutualofomaha.iaa.bean.agreement.Agreement" cascade="all" not-null="true" insert="true" update="false" lazy="false" >
<column name="AGRMT_ID" />
</many-to-one>

LOGIC
Open session
load object
add or remove items from PARENTS SET.
session.saveOrUpdate
session.flush
Close session

I just don't get why I am having so much difficulty with such a basic concept.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 1:02 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
What I meant before is, either use inverse="false" and set update="false", or inverse="true" and update="true". But this does not seem to be the problem. Could you paste the code, where you delete the entities?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 1:32 pm 
Newbie

Joined: Mon Jan 26, 2009 12:14 pm
Posts: 8
app.getAgreementProperties().remove(agreementProperty);

This is all I am doing.

Basically we have a mapping class where we evaluate the current graph and add items,update existing items or remove items. When we are all done then we do session.saveOrUpdate and then flush.

agreementProperties is a SET.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 6:26 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
So do you map your loaded set into another "mapping class" and when saving it, map it back into your Agreement-entity? Changing the collection could be the reason why delete-orphan is not working.

Could you provide your full load-edit-save code or example code if that is to much code?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 9:17 am 
Newbie

Joined: Mon Jan 26, 2009 12:14 pm
Posts: 8
This is what we have changed now during testing and we are very close.

PARENT
<!-- bi-directional one-to-many association to AgreementProperty -->
<set name="AgreementProperties" lazy="false" inverse="false" cascade="all-delete-orphan">
<key column="AGRMT_ID"/>
<one-to-many class="com.mutualofomaha.iaa.bean.agreement.AgreementProperty" />
</set>
CHILD
<many-to-one name="Agreement" foreign-key="AGRMT_ID" class="com.mutualofomaha.iaa.bean.agreement.Agreement" cascade="save-update" not-null="true" insert="true" update="false" lazy="false" >
<column name="AGRMT_ID" />
</many-to-one>

When we remove the object from the parent set. Hibernate is issuing an UPDATE on the child to sets its parents key to null. This is obviously causing an error since the child table can't have nullabel foreign key. Going to try explicitly deleting the object next.

The code to load the object is HQL on the top level object of the graph and we have lazy=false on everything.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 9:26 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
DravenJ wrote:
When we remove the object from the parent set. Hibernate is issuing an UPDATE on the child to sets its parents key to null. This is obviously causing an error since the child table can't have nullabel foreign key.


Ok, that looks like inverse="false" is working but delete-orphan is not. Try to use cascade="all,delete-orphan" instead of "all-delete-orphan" (Even though both ways are mentioned in reference guide).

_________________
-----------------
Need advanced help? http://www.viada.eu


Last edited by mmerder on Tue Jan 27, 2009 9:28 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 9:27 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Sorry, double post.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 9:38 am 
Newbie

Joined: Mon Jan 26, 2009 12:14 pm
Posts: 8
K, Did that, still issuing this update statement

update UBRENBDS.T_AGREEMENT_PROPERTY set AGRMT_ID=null where AGRMT_ID=? and AGRMT_PPTY_ID=?

The cascade property is = none for child now. This is almost the behavior I want. Just want that update statement to be a delete statement!

Now if I try and delete the AgreementProperty object through the session and not remove from parents set, I get an error stating I need to remove the object otherwise save will re-save the object.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 11:55 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I think your problem is very much like described in tutorial.

Ok, so just test to make the relationship inverse. That means, that Child is the owner of the association. You have to set inverse="true" and make the many-to-one end insert="true" and update="true". After you did that, delete-orphan should work as described.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 12:49 pm 
Newbie

Joined: Mon Jan 26, 2009 12:14 pm
Posts: 8
Thanks! Yes that works now!!!!


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