persistence@glassfish.java.net

Re: Getting error Glassfish + JPA + Jersey

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Fri, 16 Oct 2009 11:53:47 -0700

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/