persistence.xml with Glassfish 3.1.1
Asked Answered
D

3

13

I am very new to glassfish, JPA and so on and I have really problems with setting that up. What I am planning to do is a simple RESTful service with a persistent backend. I am using glassfish3 as application server and already deployed a simple REST service with the jersey-library. Now I want to provide access to a database via JPA. Glassfish is shipped with JavaDB/derby and EclipseLink, is that right? So, I want to use that :-)

I created a persistence.xml in META-INF:

<?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="myPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.driver"   value="org.apache.derby.jdbc.ClientDataSource" /> <!-- org.apache.derby.jdbc.EmbeddedDriver -->
      <property name="javax.persistence.jdbc.url"      value="jdbc:derby://localhost:1527/sample;create=true" />
      <property name="javax.persistence.jdbc.user"     value="APP" />
      <property name="javax.persistence.jdbc.password" value="APP" />
      <property name="eclipselink.ddl-generation"      value="create-tables" />
    </properties>
  </persistence-unit>
</persistence>

Then I created a field in my resource, where I want to access/store som data:

@PersistenceUnit(unitName = "myPU")
EntityManagerFactory emf;

But "emf" is always NULL :-(

I guess that my persistence.xml is not configured appropriate.

Would be really glad if someone has a hint, what I am doing wrong...

thanks!

Danby answered 4/2, 2012 at 0:40 Comment(0)
C
12

I think it is better to create JNDI for db connection . You can do it easly with GlassFish.

Firstly create connection pool (you will set db connection settings);

Resources->JDBC->JDBC Connection Pools

After that crate JNDI name for this pool ;

Resources->JDBC->JDBC Resources

So lets say you set JNDI name as "dbCon"

And here your persistence.xml ;

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
  <persistence-unit name="myPU" transaction-type="JTA">
    <jta-data-source>dbCon</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

Note : You must copy your jdbc jar to \glassfish-3.1.1\glassfish\domains\domain1\lib\ext

Champerty answered 4/2, 2012 at 1:47 Comment(6)
Thanks for your quick response. I am using the default derbypool of glassfish and changed the line to <jta-data-source>jdbc/__default</jta-data-source> also added the derby.jar to that folder. Additionally I annotated my class with Stateless and LocalBean, that is necessary, right? Now the EntityManer is not NULL anymore, but I am not able to persist any data, always get this error: javax.servlet.ServletException: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName myPUDanby
did you tried to ping database on jdbc connection pools -> DerbyPool from glassfish admin page . If you get same error when you try to ping and if this error exist "java.lang.ClassNotFound" , you can copy your derby.jar to glassfish-3.1.1\glassfish\lib .Champerty
Ping does work with the database. I found the solution now, will post it soon.. your advice pushed me into the right direction :) thanks!Danby
Hey, one more question: it seems that you are quite good at glassfish :) my data is always lost, after I recompile my code.. it seems that the database is not persistent.. =/ is there something missing in my configuration?Danby
you set persistence.xml properties as drop-and-create-tables . This will drop all tables and recreate when you compile your code . If you don't want that, you can change it as "create" or "none" .Champerty
ah, ok... i thougth it will only drop-and-create if there are any changes to the entities, but not every time... thanks again!Danby
D
5

I have the solution now for my problem. Here is the corresponding configuration:

  • glassfish 3.1.1
  • built-in JavaDB/derby database: jdbc/__default
  • glassfish's JPA, which is eclipselink
  • (JAX RS: Jersey, which is shipped with glassfish)

So, you have to create the folder "META-INF" wihtin your src folder and put the persistence.xml there:

<?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="myPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/__default</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
    </properties>
  </persistence-unit>
</persistence>

I created the .xml previously in the META-INF of WebContent, and that is wrong. You also do not have to reference any additional libraries, since you have the glassfish modules added.

Now I have created a JavaBean, where I do inject the PersistenceUnit:

@Stateless
public class StorageService {

    @PersistenceContext(unitName = "myPU")
    EntityManager em;

...
}

And this one is injected in my Resource-Classes of the Jersey-Servlets:

@Path("/someres")
@Produces(MediaType.APPLICATION_XML)
@Stateless
public class SomeRes {

    @EJB
    StorageService storageService;

...
}

The injections do only work if the classes are marked as "@Stateless".

Danby answered 7/2, 2012 at 15:44 Comment(1)
It doesn't matter where persistence.xml is in your project, it matter where it gets deployed. It should reside in app.war:/WEB-INF/classes/META-INF/persistence.xml but it was ending up in app.war:/META-INF/persistence.xml is suspect.Betel
F
0

I have not tried with RESTful service, but I guess that should not matter. I noticed you are using persistence.xml for version 1. Any specific reason?

Following persistence.xml works for me:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="myPU">
        <properties>
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
        </properties>
    </persistence-unit>
</persistence>

Hope this helps.

Felisha answered 7/2, 2012 at 15:26 Comment(1)
no, there is no reason ^^ I want to use JPA 2.0, thanks for the hint!Danby

© 2022 - 2024 — McMap. All rights reserved.