Getting error Glassfish + JPA + Jersey

View: New views
7 Messages — Rating Filter:   Alert me  

Getting error Glassfish + JPA + Jersey

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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/

Re: Getting error Glassfish + JPA + Jersey

by Mitesh Meswani :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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/


Re: Getting error Glassfish + JPA + Jersey

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
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/

Re: Getting error Glassfish + JPA + Jersey

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

For example using CMT = container managed transaction or the best practice to avoid problems...

2009/10/16 Roan Brasil Monteiro <roanbrasil@...>
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@...>

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/

Re: Getting error Glassfish + JPA + Jersey

by Roan Brasil Monteiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How can I get the EntityManager from JNDI?

2009/10/16 Roan Brasil Monteiro <roanbrasil@...>
For example using CMT = container managed transaction or the best practice to avoid problems...

2009/10/16 Roan Brasil Monteiro <roanbrasil@...>

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@...>

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/

Re: Getting error Glassfish + JPA + Jersey

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You look it up :)

Roan Brasil Monteiro wrote:

> How can I get the EntityManager from JNDI?
>
> 2009/10/16 Roan Brasil Monteiro <roanbrasil@...
> <mailto:roanbrasil@...>>
>
>     For example using CMT = container managed transaction or the best
>     practice to avoid problems...
>
>     2009/10/16 Roan Brasil Monteiro <roanbrasil@...
>     <mailto:roanbrasil@...>>
>
>         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@...
>         <mailto:Mitesh.Meswani@...>>
>
>             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/


Re: Getting error Glassfish + JPA + Jersey

by Michael Bar-Sinai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try getting the EntityManager via injection:

 @PersistenceContext(unitName = "citespacePU")
EntityManager em

-- 
Michael

On Sat, Oct 17, 2009 at 1:04 AM, Marina Vatkina <Marina.Vatkina@...> wrote:
You look it up :)

Roan Brasil Monteiro wrote:
How can I get the EntityManager from JNDI?

2009/10/16 Roan Brasil Monteiro <roanbrasil@... <mailto:roanbrasil@...>>


   For example using CMT = container managed transaction or the best
   practice to avoid problems...

   2009/10/16 Roan Brasil Monteiro <roanbrasil@...
   <mailto:roanbrasil@...>>


       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@...
       <mailto:Mitesh.Meswani@...>>


           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/