persistence@glassfish.java.net

RE: Not cascading remove

From: Dru Devore <ddevore_at_duckhouse.us>
Date: Tue, 25 Mar 2008 06:14:52 -0700
Ok, here is more detail. The tables are created by toplink directly from the code. There is only a reference, in code, from the order to the item. The tables are basically the following, not exactly because they are much larger, it appears that your test tables are the same as mine.

order
id (primary key)
other_field1
other_field2

order_order_item
order_id
orderItemList_id (The field in the code is named orderItemList)

order_item
id
other_field1
other_field2

When it deletes it makes it to the order_order_item but not on down to the order_item. Now you may ask why I did the structure this way. I will answer that question. That is the way JPA did it. I created the entities to have one way communication because I didn't want a query on the order_item to go back up to the order, there are more tables after order_item and there can be a lot of order_items for an order. Anyway I couldn't find a way to make the tables work with the entities correctly any other way and have the one way communications. But now I have this delete problem. I have considered going back to the 2 way communication and having the up lazy while keeping the down eager.

---
Dru Devore


-------- Original Message --------
Subject: Re: Not cascading remove
From: "Wonseok Kim" <guruwons@gmail.com>
Date: Tue, March 25, 2008 8:23 am
To: persistence@glassfish.dev.java.net

Hi Dru,

When I retested cascaded remove with Order and Item entities which are similar to yours, there was no problem.
I guess there is something missing in your code. Could you explain your code in more detail and tell us what version you're using.

BTW, you should see normally working logs like below.

[TopLink Finer]: 2008.03.25 09:10:11.618--ServerSession(1012673)--Thread(Thread[main,5,main])--client acquired
[TopLink Finest]: 2008.03.25 09:10:11.629--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Execute query DoesExistQuery()
[TopLink Finest]: 2008.03.25 09:10:11.630--UnitOfWork(12376621)--Thread(Thread[main,5,main])--PERSIST operation called on: jpatest.Order@67064.
[TopLink Finer]: 2008.03.25 09:10:11.633--ClientSession(1043272)--Connection(10884088)--Thread(Thread[main,5,main])--begin transaction
[TopLink Finest]: 2008.03.25 09:10:11.634--ClientSession(1043272)--Thread(Thread[main,5,main])--Execute query DataModifyQuery()
[TopLink Fine]: 2008.03.25 09:10:11.635--ClientSession(1043272)--Connection(10884088)--Thread(Thread[main,5,main])--UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
    bind => [50, SEQ_GEN]
[TopLink Finest]: 2008.03.25 09:10:11.643--ClientSession(1043272)--Thread(Thread[main,5,main])--Execute query ValueReadQuery()
[TopLink Fine]: 2008.03.25 09:10:11.644--ClientSession(1043272)--Connection(10884088)--Thread(Thread[main,5,main])--SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = ?
    bind => [SEQ_GEN]
[TopLink Finest]: 2008.03.25 09:10:11.651--ClientSession(1043272)--Connection(10884088)--Thread(Thread[main,5,main])--local sequencing preallocation for SEQ_GEN: objects: 50 , first: 1, last: 50
[TopLink Finer]: 2008.03.25 09:10:11.653--ClientSession(1043272)--Connection(10884088)--Thread(Thread[main,5,main])--commit transaction
[TopLink Finest]: 2008.03.25 09:10:11.656--ServerSession(1012673)--Connection(10884088)--Thread(Thread[main,5,main])--local sequencing preallocation is copied to preallocation after transaction commit
[TopLink Finest]: 2008.03.25 09:10:11.657--UnitOfWork(12376621)--Thread(Thread[main,5,main])--assign sequence to the object (1 -> jpatest.Order@67064)
[TopLink Finest]: 2008.03.25 09:10:11.662--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Execute query DoesExistQuery()
[TopLink Finest]: 2008.03.25 09:10:11.663--UnitOfWork(12376621)--Thread(Thread[main,5,main])--PERSIST operation called on: jpatest.Item@b03be0.
[TopLink Finest]: 2008.03.25 09:10:11.664--UnitOfWork(12376621)--Thread(Thread[main,5,main])--assign sequence to the object (2 -> jpatest.Item@b03be0)
[TopLink Finest]: 2008.03.25 09:10:11.667--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Execute query DoesExistQuery()
[TopLink Finest]: 2008.03.25 09:10:11.674--UnitOfWork(12376621)--Thread(Thread[main,5,main])--PERSIST operation called on: jpatest.Item@113a53d.
[TopLink Finest]: 2008.03.25 09:10:11.681--UnitOfWork(12376621)--Thread(Thread[main,5,main])--assign sequence to the object (3 -> jpatest.Item@113a53d)
[TopLink Finer]: 2008.03.25 09:10:11.693--ClientSession(1043272)--Connection(20358204)--Thread(Thread[main,5,main])--begin transaction
[TopLink Finest]: 2008.03.25 09:10:11.693--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Execute query InsertObjectQuery(jpatest.Item@113a53d)
[TopLink Finest]: 2008.03.25 09:10:11.695--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Assign return row DatabaseRecord(
    TEST_ITEM.ITEM_VERSION => 1)
[TopLink Fine]: 2008.03.25 09:10:11.696--ClientSession(1043272)--Connection(20358204)--Thread(Thread[main,5,main])--INSERT INTO TEST_ITEM (ID, NAME, ITEM_VERSION, DESCRIPTION) VALUES (?, ?, ?, ?)
    bind => [3, Ballpen, 1, Ballpen]
[TopLink Finest]: 2008.03.25 09:10:11.712--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Execute query InsertObjectQuery(jpatest.Item@b03be0)
[TopLink Finest]: 2008.03.25 09:10:11.712--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Assign return row DatabaseRecord(
    TEST_ITEM.ITEM_VERSION => 1)
[TopLink Fine]: 2008.03.25 09:10:11.713--ClientSession(1043272)--Connection(20358204)--Thread(Thread[main,5,main])--INSERT INTO TEST_ITEM (ID, NAME, ITEM_VERSION, DESCRIPTION) VALUES (?, ?, ?, ?)
    bind => [2, Pencil, 1, Pencil]
[TopLink Finest]: 2008.03.25 09:10:11.715--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Execute query InsertObjectQuery(jpatest.Order@67064)
[TopLink Finest]: 2008.03.25 09:10:11.716--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Assign return row DatabaseRecord(
    TEST_ORDER.ORDER_VERSION => 1)
[TopLink Fine]: 2008.03.25 09:10:11.717--ClientSession(1043272)--Connection(20358204)--Thread(Thread[main,5,main])--INSERT INTO TEST_ORDER (ORDER_ID, SHIP_ADDR, ORDER_VERSION, QUANTITY) VALUES (?, ?, ?, ?)
    bind => [1, null, 1, 0]
[TopLink Finest]: 2008.03.25 09:10:11.728--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Execute query DataModifyQuery()
[TopLink Fine]: 2008.03.25 09:10:11.735--ClientSession(1043272)--Connection(20358204)--Thread(Thread[main,5,main])--INSERT INTO TEST_ORDER_TEST_ITEM (items_ID, Order_ORDER_ID) VALUES (?, ?)
    bind => [2, 1]
[TopLink Finest]: 2008.03.25 09:10:11.751--UnitOfWork(12376621)--Thread(Thread[main,5,main])--Execute query DataModifyQuery()
[TopLink Fine]: 2008.03.25 09:10:11.751--ClientSession(1043272)--Connection(20358204)--Thread(Thread[main,5,main])--INSERT INTO TEST_ORDER_TEST_ITEM (items_ID, Order_ORDER_ID) VALUES (?, ?)
    bind => [3, 1]
[TopLink Finer]: 2008.03.25 09:10:11.755--UnitOfWork(12376621)--Thread(Thread[main,5,main])--begin unit of work commit
[TopLink Finer]: 2008.03.25 09:10:11.768--ClientSession(1043272)--Connection(20358204)--Thread(Thread[main,5,main])--commit transaction
[TopLink Finer]: 2008.03.25 09:10:11.771--UnitOfWork(12376621)--Thread(Thread[main,5,main])--end unit of work commit
[TopLink Finer]: 2008.03.25 09:10:11.771--UnitOfWork(12376621)--Thread(Thread[main,5,main])--resume unit of work
[TopLink Finer]: 2008.03.25 09:10:11.773--ServerSession(1012673)--Thread(Thread[main,5,main])--client acquired
[TopLink Finest]: 2008.03.25 09:10:11.774--UnitOfWork(28259286)--Thread(Thread[main,5,main])--Merge clone with references jpatest.Order@67064
[TopLink Finest]: 2008.03.25 09:10:11.778--UnitOfWork(28259286)--Thread(Thread[main,5,main])--Register the existing object jpatest.Item@c5495e
[TopLink Finest]: 2008.03.25 09:10:11.779--UnitOfWork(28259286)--Thread(Thread[main,5,main])--Register the existing object jpatest.Item@2af081
[TopLink Finer]: 2008.03.25 09:10:11.786--ClientSession(1043272)--Thread(Thread[Finalizer,8,system])--client released
[TopLink Finest]: 2008.03.25 09:10:11.786--UnitOfWork(28259286)--Thread(Thread[main,5,main])--The remove operation has been performed on: jpatest.Order@16c9867
[TopLink Finest]: 2008.03.25 09:10:11.787--UnitOfWork(28259286)--Thread(Thread[main,5,main])--The remove operation has been performed on: jpatest.Item@1ce784b
[TopLink Finest]: 2008.03.25 09:10:11.787--UnitOfWork(28259286)--Thread(Thread[main,5,main])--The remove operation has been performed on: jpatest.Item@c79809
[TopLink Finer]: 2008.03.25 09:10:11.787--UnitOfWork(28259286)--Thread(Thread[main,5,main])--begin unit of work commit
[TopLink Finer]: 2008.03.25 09:10:11.789--ClientSession(19432672)--Connection(213274)--Thread(Thread[main,5,main])--begin transaction
[TopLink Finest]: 2008.03.25 09:10:11.789--UnitOfWork(28259286)--Thread(Thread[main,5,main])--Execute query DeleteObjectQuery(jpatest.Order@16c9867)
[TopLink Finest]: 2008.03.25 09:10:11.792--UnitOfWork(28259286)--Thread(Thread[main,5,main])--Execute query DataModifyQuery()
[TopLink Fine]: 2008.03.25 09:10:11.793--ClientSession(19432672)--Connection(213274)--Thread(Thread[main,5,main])--DELETE FROM TEST_ORDER_TEST_ITEM WHERE (Order_ORDER_ID = ?)
    bind => [1]
[TopLink Fine]: 2008.03.25 09:10:11.805--ClientSession(19432672)--Connection(213274)--Thread(Thread[main,5,main])--DELETE FROM TEST_ORDER WHERE ((ORDER_ID = ?) AND (ORDER_VERSION = ?))
    bind => [1, 1]
[TopLink Finest]: 2008.03.25 09:10:11.853--UnitOfWork(28259286)--Thread(Thread[main,5,main])--Execute query DeleteObjectQuery(jpatest.Item@1ce784b)
[TopLink Fine]: 2008.03.25 09:10:11.856--ClientSession(19432672)--Connection(213274)--Thread(Thread[main,5,main])--DELETE FROM TEST_ITEM WHERE ((ID = ?) AND (ITEM_VERSION = ?))
    bind => [2, 1]
[TopLink Finest]: 2008.03.25 09:10:11.876--UnitOfWork(28259286)--Thread(Thread[main,5,main])--Execute query DeleteObjectQuery(jpatest.Item@c79809)
[TopLink Fine]: 2008.03.25 09:10:11.877--ClientSession(19432672)--Connection(213274)--Thread(Thread[main,5,main])--DELETE FROM TEST_ITEM WHERE ((ID = ?) AND (ITEM_VERSION = ?))
    bind => [3, 1]
[TopLink Finer]: 2008.03.25 09:10:11.879--ClientSession(19432672)--Connection(213274)--Thread(Thread[main,5,main])--commit transaction
[TopLink Finer]: 2008.03.25 09:10:11.880--UnitOfWork(28259286)--Thread(Thread[main,5,main])--end unit of work commit
[TopLink Finer]: 2008.03.25 09:10:11.881--UnitOfWork(28259286)--Thread(Thread[main,5,main])--resume unit of work

Cheers,
-Wonseok

On Tue, Mar 25, 2008 at 11:36 AM, Dru Devore <ddevore@duckhouse.us> wrote:
actually I am calling em.remove(em.merge(order)) now I was under the impression that the cascade=CascadeType.ALL indicated that a remove would remove all the through the relationship not just the intermediary table.


---
Dru Devore



-------- Original Message --------
Subject: Re: Not cascading remove
From: Marina Vatkina <Marina.Vatkina@Sun.COM>
Date: Mon, March 24, 2008 5:22 pm
To: persistence@glassfish.dev.java.net

Dru,

Are you calling em.remove(order) or (replace my names with the actual ones)
order.getItems().remove(item)?

If the latter, it only removes the relationships, not the entities.

Regards,
-marina

Dru Devore wrote:
> I am using ejb 3.0 with an order table and order item table. Wanting to
> see how it works allowing it to create the linking table, and actually
> liking I decided to let it be and worry about more important thing. What
> I have is this
>
> order (the main table)
> order_order_item (the linking table)
> order_item (the item table)
>
> When I do a remove it takes it out ofthe linking table but not out of
> the order_item table.
>
> This is how it looks in the order entity
>
> @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
> private List<OrderItem> orderItemList;
>
> It will add them and query them but it does not want to remove them.
> What am I doing wrong?
>
> ---
> Dru Devore