Try getting the EntityManager via injection:
@Persistence*Context*(unitName = "citespacePU")
*EntityManager em*;
--
Michael
On Sat, Oct 17, 2009 at 1:04 AM, Marina Vatkina <Marina.Vatkina_at_sun.com>wrote:
> You look it up :)
>
> Roan Brasil Monteiro wrote:
>
>> How can I get the EntityManager from JNDI?
>>
>> 2009/10/16 Roan Brasil Monteiro <roanbrasil_at_gmail.com <mailto:
>> roanbrasil_at_gmail.com>>
>>
>> For example using CMT = container managed transaction or the best
>> practice to avoid problems...
>>
>> 2009/10/16 Roan Brasil Monteiro <roanbrasil_at_gmail.com
>> <mailto:roanbrasil_at_gmail.com>>
>>
>> Hey ,
>>
>> Thanks thats idea worked for me. You helped me a lot. I have
>> another question, what is the right way to do that or the best
>> way to persist?
>>
>> 2009/10/16 Mitesh Meswani <Mitesh.Meswani_at_sun.com
>> <mailto:Mitesh.Meswani_at_sun.com>>
>>
>>
>> Application managed entitymanagers need to be either created
>> inside an active transaction or you need to explicitly call
>> joinTransaction() to make it part of active transaction.
>> Modify your code as follows
>>
>> EntityManager em = emf.createEntityManager();
>> //Either move this after utx.begin() below
>>
>> UserTransaction utx = getUtx();
>> try {
>> utx.begin();
>> //Or call following
>> em.joinTransaction();
>>
>>
>> em.persist(p);
>> utx.commit();
>>
>>
>> Roan Brasil Monteiro wrote:
>>
>> Dont persist and dont show error . The tables are
>> created but when I persist I cannot and got it. Bellow
>> is my code. The table Place is created but dont persist.
>> Why?
>>
>> My class Test.java
>>
>> @Path("/test")
>> public class Test {
>>
>> @PersistenceUnit(unitName = "citespacePU")
>> EntityManagerFactory emf;
>> @GET
>> @Produces("text/plain")
>> public String getIt() throws NamingException {
>> StringBuilder strb = new StringBuilder();
>> Place p = new Place();
>> p.setName("Roan");
>>
>> EntityManager em =
>> emf.createEntityManager();
>> UserTransaction utx =
>> getUtx(); try {
>> utx.begin();
>> em.persist(p);
>> utx.commit();
>> } catch (Exception e) {
>> try {
>> utx.rollback();
>> } catch (SystemException se) {
>> throw new WebApplicationException(se);
>> }
>> throw new WebApplicationException(e);
>> } finally {
>> em.close();
>> }
>>
>>
>> persistence.xml
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <persistence version="1.0"
>> xmlns="http://java.sun.com/xml/ns/persistence"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
>> http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
>> ">
>> <persistence-unit name="citespacePU"
>> transaction-type="JTA">
>>
>> <provider>org.hibernate.ejb.HibernatePersistence</provider>
>> <jta-data-source>jdbc/TestDS</jta-data-source>
>> <properties> <property
>> name="hibernate.hbm2ddl.auto" value="update"/>
>> <property
>> name="hibernate.connection.characterEncoding"
>> value="UTF-8"/>
>> </properties>
>> </persistence-unit>
>> </persistence>
>>
>>
>> -- Atenciosamente,
>>
>> Roan Brasil Monteiro
>> http://roanbrasil.wordpress.com/
>>
>>
>>
>>
>>
>> -- Atenciosamente,
>>
>> Roan Brasil Monteiro
>> http://roanbrasil.wordpress.com/
>>
>>
>>
>>
>> -- Atenciosamente,
>>
>> Roan Brasil Monteiro
>> http://roanbrasil.wordpress.com/
>>
>>
>>
>>
>> --
>> Atenciosamente,
>>
>> Roan Brasil Monteiro
>> http://roanbrasil.wordpress.com/
>>
>
>