Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

persistence.xml with Glassfish 3.1.1

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!

like image 294
Eddy Avatar asked Feb 04 '12 00:02

Eddy


People also ask

Where do I put persistence xml?

If you package the persistent unit as a set of classes in an EJB JAR file, persistence. xml should be put in the EJB JAR's META-INF directory. If you package the persistence unit as a set of classes in a WAR file, persistence. xml should be located in the WAR file's WEB-INF/classes/META-INF directory.

What is persistence xml?

persistence. xml defines one or more persistence units. The following is an example persistence. xml file. <persistence> <persistence-unit name="OrderManagement"> <description>This unit manages orders and customers.

Why do we need persistence xml?

xml File. This file is used to override the default Hibernate settings and to add support for database types that are not out of the box (OOB database types are Oracle Server, Microsoft SQL Server, and MySQL).

Can we have multiple persistence xml?

xml files. Have multiple persistence units in one persistence.


1 Answers

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

like image 63
Jman Avatar answered Oct 12 '22 14:10

Jman