File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Query on Detached and Removed Entities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Query on Detached and Removed Entities" Watch "Query on Detached and Removed Entities" New topic
Author

Query on Detached and Removed Entities

Sriram Kannan
Greenhorn

Joined: Nov 10, 2008
Posts: 11
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

Joined: Jul 05, 2005
Posts: 1936
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.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Stefan Taranu
Greenhorn

Joined: Feb 06, 2009
Posts: 22
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?




    Stefan
    Hong Anderson
    Ranch Hand

    Joined: Jul 05, 2005
    Posts: 1936
    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://aspose.com/file-tools
     
    subject: Query on Detached and Removed Entities