Case 2 works. No Error, and the item is not removed, but it is added to the end of the table (looks like re-added).
What makes the difference in these cases ? Is it the difference between the detached and the removed entities ? Because in Case 1, the Entity State is "removed" when the merge happens. But, in Case 2, the Entity State is "detached".
This gives me a real confusion about how remove() works now. Could anyone please throw a light on this ?
Looking at your client code I understand the following
- Item item = itemFacade.findItemByName("item1"); - it calls the Bean's method findItemByName which returns a detached Item entity (supposing it finds it)
- itemFacade.remove(item); - calls the bean's method which calls:
em.merge(item) - merges the detached entity with your DB, i.e. it adds to DB, and returns a managed entity
em.remove (em.merge(item)) - removes the managed entity
- itemFacade.merge(item); - which will take your detached item and merge it with the DB, i.e it will be added to the DB. That's why you see it again in the database.
I am curious, what happens if you run the client code multiple times. It adds multiple entries to you Database? Or is always just one?
Stefan
Hong Anderson
Ranch Hand
Joined: Jul 05, 2005
Posts: 1936
posted
0
Stefan Taranu wrote:
I am curious, what happens if you run the client code multiple times. It adds multiple entries to you Database? Or is always just one?
Just once, because it removes first and then merges the detached entity.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.