Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on Detached and Removed Entities

 
Sriram Kannan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am quite new to the EJB 3.0, which I am learning and working out examples on my own whereupon I stumbled upon this strange problem.

I was checking the exception when trying to merge an entity after removing it.

I tried it in 2 cases. One worked fine (throws an IllegalArgumentException), but the other does not.

I am using NetBeans 6.5 and GlassFish Server v2.1 for the below code.

Case 1 : (A simple one, written in the Entity Class itself. This throws an IllegalArgumentException)

@PersistenceContext EntityManager em;
public void test (Item item) {

item = em.merge(item);
em.remove(item);
item = em.merge(item);

}

Case 2 : (This was written from a standalone client, that access the entity through an ItemFacade Session Bean)

@Stateless
public class ItemFacade implements ItemFacadeRemote {

@PersistenceContext
private EntityManager em;
....
...
..

public Item findItemByName(String itemName) {

return (Item) em.createQuery ("select i from Item as i where item.name = \'" + itemName + "\'").getSingleResult();
}

public void merge(Item item) {

em.merge(item);
}

public void remove(Item item) {

em.remove(em.merge(item));
}


}

//Client code

ItemFacade itemFacade;
Item item = itemFacade.findItemByName("item1");
itemFacade.remove(item);
itemFacade.merge(item);


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 ?

Thanks a lot :-)
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it the difference between the detached and the removed entities ?

Correct.

From API documentation:
merge

<T> T merge(T entity)

Merge the state of the given entity into the current persistence context.

Parameters:
entity -
Returns:
the instance that the state was merged to
Throws:
IllegalStateException - if this EntityManager has been closed.
IllegalArgumentException - if instance is not an entity or is a removed entity

Merge can also be used to integrate a detached entity into a persistent context.
 
Stefan Taranu
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sriram Kannan wrote:Hi all,
....

...


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?


     
    Hong Anderson
    Ranch Hand
    Posts: 1936
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic