How to use the Entity Manager correctly?

Hello

I would like to ask a EntityManager related question. From the tutorials I have read I came across two different categories of examples in which EntityManager is used. In the first type it is like:

EntityManager em;

publicvoid addMethod(Object o)

{

em.persist(o);

}

whereas in the second type of examples, it is like:

EntityManager em;

publicvoid addMethod(Object o)

{

em.getTransaction().begin();

em.persist(o);

em.commit();

}

My question is, what is the difference between the two different approaches?

Thanks

[858 byte] By [rabbiaa] at [2007-11-26 16:52:24]
# 1
hello allFinally i think i found the solution. Please correct me if i am wrong.The second approach is used when you are not using the container managed transactions (JTA Transaction).Regards
rabbiaa at 2007-7-8 23:20:05 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Correct.The Java Persistence API has its own local transaction API. Typicallyit's better to not use that and instead interact with the EntityManager within thescope of a global transaction. --ken
ksaksa at 2007-7-8 23:20:05 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
thanks alot!
rabbiaa at 2007-7-8 23:20:05 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...