Monday, 8 October 2012

Class.getResourceAsStream() VS. ClassLoader.getResourceAsStream()


For Class.getResourceAsStream(String name), if the name parameter doesn't start with a "/", then it's a relative path to the class's package. If the name parameter starts with a "/", then it's an absolute path.

For ClassLoader.getResourceAsStream(String name), the name parameter is always an absolute path, and it can never start with a "/". If it does, the resource is never found.

If the file cannot be found, both methods return null and no exception is thrown.

The following program illustrates the difference.

Project structure











ResourceAsStream.java

public class ResourceAsStream {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String path1 = "a.properties";
        String path2 = "/a.properties";
        String path3 = "test/a.properties";
        String path4 = "/test/a.properties";
  
        System.out.println("Class.getResourceAsStream()");
        InputStream is = ResourceAsStream.class.getResourceAsStream(path1);
        System.out.println(path1 + " " + (is != null));
        is = ResourceAsStream.class.getResourceAsStream(path2);
        System.out.println(path2 + " " + (is != null));
        is = ResourceAsStream.class.getResourceAsStream(path3);
        System.out.println(path3 + " " + (is != null));
        is = ResourceAsStream.class.getResourceAsStream(path4);
        System.out.println(path4 + " " + (is != null));
        System.out.println();
        System.out.println("ClassLoader.getResourceAsStream()");
        is = ResourceAsStream.class.getClassLoader().getResourceAsStream(path1);
        System.out.println(path1 + " " + (is != null));
        is = ResourceAsStream.class.getClassLoader().getResourceAsStream(path2);
        System.out.println(path2 + " " + (is != null));
        is = ResourceAsStream.class.getClassLoader().getResourceAsStream(path3);
        System.out.println(path3 + " " + (is != null));
        is = ResourceAsStream.class.getClassLoader().getResourceAsStream(path4);
        System.out.println(path4 + " " + (is != null));
  
    }

}


Result:

Class.getResourceAsStream()
a.properties true
/a.properties false
test/a.properties false
/test/a.properties true

ClassLoader.getResourceAsStream()
a.properties false
/a.properties false
test/a.properties true
/test/a.properties false


Thursday, 4 October 2012

JSF 2.0 integrated with Spring 3 through annotation

JSF can certainly be integrated with Spring 3 through XML declaration, but this article illustrates the simplest way, annotation, to get the job done.

Project structure



















pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>jsf-spring</artifactId>
    <packaging>war</packaging>
    <name>JSF Spring</name>
    <version>0.1</version>

    <dependencies>
  
        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>

        <!-- JSF -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.13</version>
        </dependency>
  
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.13</version>
        </dependency>
  
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>jsf-spring-web</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <!-- JSF Mapping -->
    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>


faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
</faces-config>


UserDaoImpl.java

package com.demo.dao;

import org.springframework.stereotype.Repository;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Override
    public String getMessage() {
        return "Hello";
    }

}


UserBean.java

package com.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.demo.dao.UserDao;

@Component("userBean")
@Scope("session")
public class UserBean {
 
    @Autowired
    private UserDao userDao;
 
    public String message(){
        return userDao.getMessage();
    }

    public UserDao getUserDao() {
        return userDao;
    }
}


Note UserBean is supposed to be the manage bean and yet there is no JSF annotation (such as @ManagedBean or @SessionScoped) at all.

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 
    <context:annotation-config/>
 
    <context:component-scan base-package="com.demo.dao" />
    <context:component-scan base-package="com.demo.controller" />
 
</beans>


index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html">

    <h:body>
        <h1>JSF 2.0 + Spring Example</h1>
            #{userBean.message()}
    </h:body>

</html>

Monday, 24 September 2012

JPA Hibernate one-to-many bidirectional mapping on List

This article illustrates how to use JPA and Hibernate annotation to do one-to-many bidirectional mapping for ordered entities.

The One side is the Teacher entity

@Entity
public class Teacher {
 
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
 
    @Column
    private String name;
 
    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @JoinColumn(name="STU_ID")
    @IndexColumn(name="IDX")
    private List<Student> students = new ArrayList<Student>();
  
    public Teacher(String name) {
        super();
        this.name = name;
    }
 
    public Teacher() {
        super();
    }

    public void addStudent(Student s){
        students.add(s);
        s.setTeacher(this);
    }

    //Setters and getters
}

Note the Teacher class has a List of Students, not a Set of Students. The @IndexColumn annotation is used to specify which column in Student table to store the index.

The Many side is the Student entity

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
 
    @Column
    private String name;
 
    @ManyToOne
    @JoinColumn(name="STU_ID", 
            insertable=false, updatable=false)
    private Teacher teacher;

    public Student(String name) {
        Super();
        this.name = name;
    }
 
    public Student() {
        super();
    }

    //Setters and getters 
}

The name attribute of @JoinColumn for Teacher and Student should match one another (in this case, it is "STU_ID").

Write a JUnit test case


public void testTeacherDao(){
    TeacherDao dao = (TeacherDao)applicationContext.getBean("teacherDao");
    Teacher teacher = new Teacher("t1");
    teacher.addStudent(new Student("s1"));
    teacher.addStudent(new Student("s2"));
    dao.save(teacher);
}            


Output:

Teacher table





Student table






This example works in Hibernate 4.1.7.

Wednesday, 19 September 2012

Spring applicationContext.xml cheat sheet for creating transaction manager for resource local persistence unit

Spring 3.0.5 + Hibernate 3.4.0

Approach 1

Specify Connection properties in persistence.xml
<persistence 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"
        version="1.0">

     <persistence-unit name="hibernate-resourceLocal" transaction-type="RESOURCE_LOCAL">
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
         <properties>
             <!-- Connection properties -->
             <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver" />
             <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb" />
             <property name="hibernate.connection.username" value="APP" />
             <property name="hibernate.connection.password" value="APP" />

             <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
             <!-- create-drop, create -->
             <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.autocommit" value="true" />
         </properties>
    </persistence-unit>
</persistence>

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx  
            http://www.springframework.org/schema/tx/spring-tx.xsd">

     <context:component-scan base-package="com.demo.dao" />

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
         <property name="persistenceUnitName" value="hibernate-resourceLocal"/>
     </bean>

     <tx:annotation-driven />
 
     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory" />
     </bean> 
 
</beans>

Approach 2

Specify Connection properties in applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx  
            http://www.springframework.org/schema/tx/spring-tx.xsd">

     <context:component-scan base-package="com.demo.dao" />

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
         <property name="dataSource" ref="dataSource"/>
         <property name="persistenceUnitName" value="default"/>
         <property name="jpaVendorAdapter">
             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                 <property name="showSql" value="true"/>
                 <property name="generateDdl" value="true"/>
                 <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
             </bean>
         </property>
     </bean>
 
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
         <property name="url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb"/>
         <property name="username" value="APP"/>
         <property name="password" value="APP"/>
     </bean>
 
     <tx:annotation-driven/>
 
     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory" />
     </bean>
</persistence-unit>



The persistence.xml is all but empty.
<persistence 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"
        version="1.0">
     <persistence-unit name="default"/>
</persistence>

Glassfish 3.1 + Spring 3.0 + JNDI

I am writing a web application deployed in Glassfish application server 3.1. The service bean is exposed as a Spring managed bean instead of an EJB. The service bean is injected with an EntityManager, the data source of which is obtained from JNDI.

Wiring Spring with JNDI in Glassfish is more difficult than I thought.

I come across some common exceptions on which there are tons of discussions online. However, I fail to find helpful answers.

Therefore I feel necessary to write this blog to provide a simple solution.

persistence.xml
<persistence 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"
     version="1.0">
     <persistence-unit name="jta" transaction-type="JTA">
         <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
         <jta-data-source>jdbc/myderby</jta-data-source>
         <properties>
             <property name="eclipselink.ddl-generation" value="create-tables" />
             <property name="eclipselink.ddl-generation.output-mode" value="database" />
             <property name="eclipselink.logging.parameters" value="true" />
             <property name="eclipselink.logging.level" value="FINE" />
         </properties>
     </persistence-unit>
</persistence>


applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx.xsd">

     <context:component-scan base-package="com.demo.controller" />
     <context:component-scan base-package="com.demo.dao" />

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
         <property name="persistenceUnitName" value="jta"/>
     </bean>

     <tx:annotation-driven />
 
     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory" />
     </bean>
  
</beans>

UserDao.java
@Repository("userDao")
public class UserDaoImpl implements UserDao {

     @PersistenceContext 
     protected EntityManager entityManager;
 
     @Transactional
     public void save(User user) {
         entityManager.persist(user);
     }
}

Deployment is fine. When dao is accessed at runtime, an exception is thrown:


java.lang.IllegalStateException:
Exception Description: Cannot use an EntityTransaction while using JTA.
     at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.getTransaction(JTATransactionWrapper.java:65)
     at org.eclipse.persistence.internal.jpa.EntityManagerImpl.getTransaction(EntityManagerImpl.java:1153)
     at org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:70)
     at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:332)
I change the transaction-type from "JTA" to "RESOURCE_LOCAL" in persistence.xml.

The result is even worse. Deployment is complaining:

SEVERE: Exception while preparing the app : The persistence-context-ref-name [com.demo.dao.UserDaoImpl/entityManager] in module [spring-mvc-jpa-jndi] resolves to a persistence unit called [jta] which is of type RESOURCE_LOCAL. Only persistence units with transaction type JTA can be used as a container managed entity manager. Please verify your application.

I am not surprised to see this exception though. Since this is a Java EE environment, the transaction type should only be JTA. So I change the transaction type back to JTA.

I then try a lot of approaches in vain until I see this article:

http://java.dzone.com/tips/how-use-glassfish-managed-jpa

The core value of the article is this single line:

<tx:jta-transaction-manager/>


I replace my transactionManager bean definition with this line and it works perfectly.

After some investigation, it turns out this line creates the transactionMananger instance from this class: org.springframework.transaction.jta.JtaTransactionManager.

So it also works if I replace this line with:

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>


Now it is easy to explain why the previous transactionManager causes an exception.  Clearly the org.springframework.orm.jpa.JpaTransactionManager is the transaction manager for RESOURCE_LOCAL persistence unit while org.springframework.transaction.jta.JtaTransactionManager is the transaction manager for JTA persistence unit. The latter probably delegates all the hassle stuff such as "beginning transaction","committing" and "closing transaction" to the Java EE container while the former needs to do all this chore.

With only one persistence unit defined, I can further strip the applicationContext.xml down to the following final version.

<context:component-scan base-package="com.demo.controller" />
<context:component-scan base-package="com.demo.dao" />

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"/>

<tx:annotation-driven />
 
<tx:jta-transaction-manager/>
I am using Spring 3.0.5.

Monday, 17 September 2012

A bug in EclipseLink

I recently found a bug in EclipseLink JPA provider.

I posted the question and answer at Stackoverflow.

Let me summarize the problem here.

First, retrieve an entity from the database, call entityManager.merge() on this entity, change the entity's Id, call entityManager.merge() on this entity again. An exception will be thrown.

Note the above operations are not executed in the same transaction context. Rather, each merge() is executed within its own transaction.

Here is the pseudocode to illustrate the idea.


User user = userManager.find(1); 
userManager.merge(user); 
System.out.println("User is managed? "+userManager.contains(user);
user.setId(2);
userManager.merge(user);


The console outputs:

User is managed? false

Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [demo.model.User] is mapped to a primary key column in the database. Updates are not allowed.


userManager.contains() methods invokes the entityManager.contains() method, the fact that it returns false indicates the entity is not being managed. When switching the JPA provider from EclipseLink to Hibernate, it works fine. And according to the JPA specification, it shouldn't throw any exception in this scenario. I believe this bug has manifested itself in other forms. I have found several articles discussing this exception. If you want to reproduce this error, this blog presents one of the simplest approaches.

Thursday, 13 September 2012

Persistence.xml cheat sheet for Hibernate, EclipseLink and OpenJPA

<persistence 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"
 version="1.0">
 
 <persistence-unit name="jta" transaction-type="JTA">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <jta-data-source>jdbc/myderby</jta-data-source>
 </persistence-unit>

 <persistence-unit name="hibernate-resourceLocal" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <properties>
   <!-- Connection properties -->
   <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver" />
   <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb" />
   <property name="hibernate.connection.username" value="APP" />
   <property name="hibernate.connection.password" value="APP" />

   <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
   <!-- create-drop, create -->
   <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.autocommit" value="true" />
  </properties>
 </persistence-unit>

 <persistence-unit name="eclipseLink-resourceLocal" transaction-type="RESOURCE_LOCAL">
  <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
  <class>com.test.entity.User</class>
  <properties>
   <!-- Connection properties -->
   <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
   <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb" />
   <property name="eclipselink.jdbc.user" value="APP" />
   <property name="eclipselink.jdbc.password" value="APP" />

   <property name="eclipselink.target-database" value="Derby" />
   <!-- drop-and-create-tables -->
   <property name="eclipselink.ddl-generation" value="create-tables" />
   <property name="eclipselink.ddl-generation.output-mode"
    value="database" />
   <property name="eclipselink.logging.parameters" value="true" />
   <property name="eclipselink.logging.level" value="FINE" />
  </properties>
 </persistence-unit>

 <persistence-unit name="openjpa-resourceLocal" transaction-type="RESOURCE_LOCAL">
  <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
  <class>com.test.entity.User</class>
  <properties>
   <!-- Connection properties -->
   <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.ClientDriver" />
   <property name="openjpa.ConnectionURL" value="jdbc:derby://localhost:1527/c:\derbydb\mydb" />
   <property name="openjpa.ConnectionUserName" value="APP" />
   <property name="openjpa.ConnectionPassword" value="APP" />

   <property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
   <property name="openjpa.jdbc.DBDictionary" value="derby" />
   <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
   <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />
  </properties>
 </persistence-unit>
</persistence>