-->
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: Update for Delete?
PostPosted: Wed Nov 23, 2005 3:28 am 
Regular
Regular

Joined: Thu Feb 17, 2005 1:58 am
Posts: 63
I was deleting objects, so how come I am seeing a delete SQL in the stack trace?

Thanks...

2005-11-22 23:25:45,113 DEBUG [ROOT] (AbstractCollectionPersister.java:947) - Deleting collection: [Store.items#b0ce5583ffffffccffffaa7f]
2005-11-22 23:25:45,113 DEBUG [ROOT] (AbstractBatcher.java:173) - reusing prepared statement
2005-11-22 23:25:45,113 DEBUG [ROOT] (AbstractBatcher.java:344) - update Item set partyOid=null where partyOid=?
2005-11-22 23:25:45,113 DEBUG [ROOT] (NullableType.java:79) - binding 'b0ce5583ffffffccffffaa7f' to parameter: 1
2005-11-22 23:25:45,113 DEBUG [ROOT] (BatchingBatcher.java:28) - Adding to batch
2005-11-22 23:25:45,113 DEBUG [ROOT] (AbstractCollectionPersister.java:977) - done deleting collection
2005-11-22 23:25:45,113 DEBUG [ROOT] (BatchingBatcher.java:55) - Executing batch size: 4
2005-11-22 23:25:45,128 DEBUG [ROOT] (NewPooledConnection.java:304) - com.mchange.v2.c3p0.impl.NewPooledConnection@15575e0 handling a throwable.
java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:642)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 3:34 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
can you post a sample of the code that causes this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 3:35 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
you are trying to delete something that is probably named like party . the party is referenced by another entity. the entity cannot keep the reference to a non-existent party, so the fk is being updated to null. this causes a constraint violation in the data model.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 3:37 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
you would get this with plain jdbc as well ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 11:12 am 
Regular
Regular

Joined: Thu Feb 17, 2005 1:58 am
Posts: 63
dennisbyrne wrote:
you are trying to delete something that is probably named like party . the party is referenced by another entity. the entity cannot keep the reference to a non-existent party, so the fk is being updated to null. this causes a constraint violation in the data model.


Is what you describe above done internally by Hibernate? If I were to use plain native SQL, I would just get a FK constraint. The DB would not issue an "update".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 12:49 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
hiberdude wrote:
Is what you describe above done internally by Hibernate? If I were to use plain native SQL, I would just get a FK constraint. The DB would not issue an "update".


Yes, this is correct.


Top
 Profile  
 
 Post subject: Same problem
PostPosted: Thu Nov 24, 2005 9:46 am 
Newbie

Joined: Tue Jul 12, 2005 5:17 am
Posts: 9
It happens when your child table has a composite key.

For instance, your tables are ORDER (order_id key), PRODUCT (product_id key) and DETAIL (order_id + product_id composite key).

When mapping DETAIL as a set, like for example:

<class name="Detail" table="DETAIL">
<composite-id>
<key-property name="order_id" />
<key-property name="product_id" />
</composite-id>
</class>
<class name="Order" table="ORDER">
<id name="order_id" />
<set name="details" cascade="all ">
<key column="product_id" />
<one-to-many class="Detail" />
</set>
</class>

You’ll get the same trace when deleting Order object:

[24/11/05 14:28:44:157 CET] 10ff10ff SystemOut U Hibernate: update DETAIL set order_id=null where order_id=?

With evidently the same exception:
[WARN] JDBCExceptionReporter - SQL Error: 1407, SQLState: 72000
[ERROR] JDBCExceptionReporter - ORA-01407: cannot update ("DETAIL"."order_id") to NULL

I played with cascade property but didn’t find the good one.


Top
 Profile  
 
 Post subject: Re: Same problem
PostPosted: Thu Nov 24, 2005 1:10 pm 
Regular
Regular

Joined: Thu Feb 17, 2005 1:58 am
Posts: 63
Hibernate 3.1rc2
Annotations 3.1beta6
JDK 1.5.05

Well, I wonder if this is allowed usage or if it is a Hibernate bug.

List<Cart> carts = cartFinder.findCarts("XYZ*");

for(Cart cart : carts)
{
for(Item item : cart.getItems())
editor.deleteItem(item);

// The below line is what causes the update() problem.
// If commented out, everything works as expected. Ie. all
// Item and Book table entries are deleted properly -- confirmed!
// So if all items associated with 'cart' are gone, why the update()
// problem?
editor.deleteCart(cart );
}

trans.commit();

editor.delete() is essentially a hibernte session.delete(obj)...
Item is the base class. Actual instance is Book.
So, Item <- Book.

Each Cart has a PK cartId.
Each Item has a itemId as PK.
Each Item has a cartId as FK (not part of item PK).


green_fr wrote:
It happens when your child table has a composite key.

For instance, your tables are ORDER (order_id key), PRODUCT (product_id key) and DETAIL (order_id + product_id composite key).

When mapping DETAIL as a set, like for example:

<class name="Detail" table="DETAIL">
<composite-id>
<key-property name="order_id" />
<key-property name="product_id" />
</composite-id>
</class>
<class name="Order" table="ORDER">
<id name="order_id" />
<set name="details" cascade="all ">
<key column="product_id" />
<one-to-many class="Detail" />
</set>
</class>

You’ll get the same trace when deleting Order object:

[24/11/05 14:28:44:157 CET] 10ff10ff SystemOut U Hibernate: update DETAIL set order_id=null where order_id=?

With evidently the same exception:
[WARN] JDBCExceptionReporter - SQL Error: 1407, SQLState: 72000
[ERROR] JDBCExceptionReporter - ORA-01407: cannot update ("DETAIL"."order_id") to NULL

I played with cascade property but didn’t find the good one.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 1:22 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Code:
editor.deleteItem(item);


don't get defensive when you read this but you may want to read a little about what 'transparent persistence' is and why ORMs like Hibernate and a few others have been so successful. It's not a good idea to put methods like save() or delete() in your domain model.


Top
 Profile  
 
 Post subject: Re: Same problem
PostPosted: Thu Nov 24, 2005 1:23 pm 
Regular
Regular

Joined: Thu Feb 17, 2005 1:58 am
Posts: 63
Never mind. I messed up the annotations.

Cart.getItems() needed a insertable=false, updatable=false.



hiberdude wrote:
Hibernate 3.1rc2
Annotations 3.1beta6
JDK 1.5.05

Well, I wonder if this is allowed usage or if it is a Hibernate bug.

List<Cart> carts = cartFinder.findCarts("XYZ*");

for(Cart cart : carts)
{
for(Item item : cart.getItems())
editor.deleteItem(item);

// The below line is what causes the update() problem.
// If commented out, everything works as expected. Ie. all
// Item and Book table entries are deleted properly -- confirmed!
// So if all items associated with 'cart' are gone, why the update()
// problem?
editor.deleteCart(cart );
}

trans.commit();

editor.delete() is essentially a hibernte session.delete(obj)...
Item is the base class. Actual instance is Book.
So, Item <- Book.

Each Cart has a PK cartId.
Each Item has a itemId as PK.
Each Item has a cartId as FK (not part of item PK).


green_fr wrote:
It happens when your child table has a composite key.

For instance, your tables are ORDER (order_id key), PRODUCT (product_id key) and DETAIL (order_id + product_id composite key).

When mapping DETAIL as a set, like for example:

<class name="Detail" table="DETAIL">
<composite-id>
<key-property name="order_id" />
<key-property name="product_id" />
</composite-id>
</class>
<class name="Order" table="ORDER">
<id name="order_id" />
<set name="details" cascade="all ">
<key column="product_id" />
<one-to-many class="Detail" />
</set>
</class>

You’ll get the same trace when deleting Order object:

[24/11/05 14:28:44:157 CET] 10ff10ff SystemOut U Hibernate: update DETAIL set order_id=null where order_id=?

With evidently the same exception:
[WARN] JDBCExceptionReporter - SQL Error: 1407, SQLState: 72000
[ERROR] JDBCExceptionReporter - ORA-01407: cannot update ("DETAIL"."order_id") to NULL

I played with cascade property but didn’t find the good one.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 1:52 pm 
Regular
Regular

Joined: Thu Feb 17, 2005 1:58 am
Posts: 63
dennisbyrne wrote:
Code:
editor.deleteItem(item);


don't get defensive when you read this but you may want to read a little about what 'transparent persistence' is and why ORMs like Hibernate and a few others have been so successful. It's not a good idea to put methods like save() or delete() in your domain model.


The delete and save is part of a "business manager" class hiearchy...ie the 'Editor'. It is intended that deleteItem will do certain validation before issuing the Hibernate session.delete()...

I always appreciate good insights...so no need to feel a need to keep gloves on :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 1:54 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
sorry about that. I thought Editor was a persistent class.


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.