Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

the type of @PersistenceContext  RSS feed

 
mojo lai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to use @PersistenceContent(type=PersistenceContentType.EXTENDED)

in my Servlet or stateless session bean I will get a NullException

Does that mean that I can use only TRANSACTION type in both servlet and stateless session bean ?
[ November 20, 2007: Message edited by: mojo lai ]
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extended persistent contexts are only valid in a stateful session bean or when obtained in an application-managed manner (obtained using getEntityManager() on a factory). You should never set the context type when in a SLSB or a servlet.
 
mojo lai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, I still have a question,
How do I use application-managed EntityManager with EXTENDED PersistenceCotext in my Servlet ?
I don't see any getEntityManager() on a factory in java EE 5 api
so I do the following , but It acts like TRANSACTION scope type

@PersistenceUnit
EntityManagerFactory emf;
EntityManager em;


public void method(){
Map map=new HashMap();
map.put("type",PersistenceContextType.EXTENDED);
em = emf.createEntityManager(map);
}
[ November 20, 2007: Message edited by: mojo lai ]
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you call createEntityManager then the result is always an EM with an extended persistence context. So you actually did more work then you even need to :-). This is all you need to do:

@PersistenceUnit
EntityManagerFactory emf;
EntityManager em;

public void method() {
em = emf.createEntityManager();
// do stuff
em.close();
}
 
mojo lai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, one more question about Transaction scope PersistenceContext,

when Transaction end, the PersistenceContext ends,too

then I do the following on a detach object

utx.begin();
em.merge(object);
utx.commit();

it will create a new PersistenceContext and then destroy it after commit() ??
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A transaction-scoped pc is only going to exist within a container-managed entity manager (one that is injected, for example), not one that you obtain from a factory, as described above.

Container-managed entity managers are normally used in container managed transactions, not with bean-managed ones as your example shows. If they are used within a bean-managed transaction, though, then you are correct, a new persistence context will be created for the duration of the transaction and then closed when the transaction commits.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!