users@glassfish.java.net

obtaining entity manager in entity

From: Bobby Bissett - Javasoft <Robert.Bissett_at_Sun.COM>
Date: Tue, 25 Sep 2007 22:35:41 -0400

According to
http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#PersistenceContext

...I can get a handle to the EntityManager in a stateless session bean with:

     @PersistenceContext
     EntityManager em;

In my case, however, I simply have an entity (class annotated with
@Entity) and I need to be able to get the EntityManager associated with
it so I can persist it. Is there any way to do this from the class
itself? Obviously my code creates an entity manager factory at some
point to deal with this, but I have an extra layer of abstraction for
dealing with certain entities and I want to be able to call:

   someObjectThatKnowsNothingAboutMyPersistenceUnit.persist(myEntity)

If it sounds too complicated, it's because I'm simplifying here. ;) To
put it simply, can an entity class instance in any way persist itself,
either by producing its own EntityManager or otherwise?

Thanks,
Bobby