persistence@glassfish.java.net

Re: Jersey + Mysql + Hibernate/JPA + Glassfish v2.1

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Thu, 15 Oct 2009 14:30:02 -0700

You are declaring @PersistenceContext on a JAX-RS resource. A plain
JAX-RS resource class isn't required to support the EE environment
annotations in Java EE 5. Move the annotation to your servlet (or
declare equivalent in web.xml). The look up should work from within
JAX-RS resource as well.

Regads,
Mitesh

Roan Brasil Monteiro wrote:
>
> What should I suppose to do to set up a project using Maven 2. I
> created a project using archetype jersey-quickstart-webapp and I
> am getting a little dificult thing to integrate with JPA/hibernate
> and Glassfish v2.1 .
>
> On Glassfish I configured the Resources and my persistence.xml is
> in src/main/resources/META-INF/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="citespace-jpa" >
> <provider>org.hibernate.ejb.HibernatePersistence</provider>
> <jta-data-source>jdbc/TestDS</jta-data-source>
> <!--
> <non-jta-data-source>java:/jdbc/citespace-jpaDS</non-jta-data-source>
> -->
> <class>com.citespace.citespacerest.Place</class>
> <properties>
> <property name="hibernate.dialect"
> value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
> <property name="hibernate.hbm2ddl.auto" value="update"/>
> <property name="hibernate.show_sql" value="true"/>
> <property name="hibernate.format_sql" value="true"/>
> <!-- <property
> name="hibernate.connection.characterEncoding" value="UTF-8"/>
> <property name="hibernate.connection.driver_class"
> value="com.mysql.jdbc.Driver"/>
> <property name="hibernate.connection.url"
> value="jdbc:mysql://localhost:3306/citespace_article"/>
> <property name="hibernate.connection.username" value="root"/>
> <property name="hibernate.connection.password"
> value="spacecite"/> -->
> </properties>
> </persistence-unit>
> </persistence>
>
>
>
> my web.xml is
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
>
> <servlet>
> <servlet-name>Jersey Web Application</servlet-name>
>
> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
>
> <!--
> <servlet-class>com.sun.jersey.impl.container.servlet.ServletAdaptor</servlet-class>
> -->
> <init-param>
>
> <param-name>com.sun.jersey.config.property.packages</param-name>
> <param-value>com.citespace.citespacerest</param-value>
> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
> <servlet-mapping>
> <servlet-name>Jersey Web Application</servlet-name>
> <url-pattern>/webresources/*</url-pattern>
> </servlet-mapping>
> <resource-ref>
> <res-ref-name>jdbc/TestDS</res-ref-name>
> <res-type>javax.sql.DataSource</res-type>
> <res-auth>Container</res-auth>
> </resource-ref>
> </web-app>
>
> and my code is:
>
> @PersistenceContext(name="persistence/citespace-jpa",
> unitName="citespace-jpa")
> @Path("/test")
> public class Test {
>
>
>
>
>
> @GET
> @Produces("text/plain")
> public String getIt() throws NamingException {
> StringBuilder strb = new StringBuilder();
>
> EntityManager em = (EntityManager) new
> InitialContext().lookup("java:comp/env/persistence/citespace-jpa");
>
> Place p = em.find(Place.class, 1);
> strb.append(p.getId()+":"+p.getName());
>
>
>
> what should I do to find a value and persist in my DB using JPA
> because on this example I am getting:
>
> ------------------------------------------------------------------------
>
> *type* Exception report
>
> *message*
>
> *description*The server encountered an internal error () that
> prevented it from fulfilling this request.
>
> *exception*
>
> javax.servlet.ServletException: com.sun.jersey.api.container.MappableContainerException: javax.naming.NameNotFoundException: No object bound to name java:comp/env/persistence/citespace-jpa
>
> *root cause*
>
> com.sun.jersey.api.container.MappableContainerException: javax.naming.NameNotFoundException: No object bound to name java:comp/env/persistence/citespace-jpa
>
> *root cause*
>
> javax.naming.NameNotFoundException: No object bound to name java:comp/env/persistence/citespace-jpa
>
>
> --
> Atenciosamente,
>
> Roan Brasil Monteiro
> http://roanbrasil.wordpress.com/
>
>
>
>
> --
> Atenciosamente,
>
> Roan Brasil Monteiro
> http://roanbrasil.wordpress.com/