Thursday, April 25, 2013

How to configure JPA project


This post shows how to configure the JPA project about its' persistence properties after entities successfully generated in a JPA project.

Before we start to configure our JPA project, first we must decide which JPA implementation to use.
There are two(2) popular JPA implementations, which are EclipseLink and Hibernate.

Once the preferred JPA implementation installed, then we can start to configure the persistence properties.

Steps:

1. Install JPA implementation library

1.1 EclipseLink

      a) download the required EclipseLink library, and the version that I used in this tutorial is 2.4.1

      b) unzip the archive

      c) copy the javax.persistence_2.0.4.v201112161009.jar and eclipselink.jar to a folder (eg, c:\eclipselink-lib)

      d) create a user libraries name eclipselink in eclipse, and add all above jar into this user library.

      e) create a user libraries for the target jdbc driver, and add the jdbc jar into this user library

creating EclipseLink library

     f) add these 2 user libraries to the JPA project.
adding EclipseLink library


1.2 Hibernate


      a) download the required Hibernate Library, and the version that I used in this tutorial is 4.2.0

      b) unzip the archive

      c) copy the jar files below to a folder (eg, c:\eclipselink-lib\hibernate)
          - antlr-2.7.7.jar
          - dom4j-1.6.1.jar
          - hibernate-commons-annotations-4.0.1.Finall.jar
          - hibernate-core-4.2.0.Final.jar
          - hibernate-entitymanager-4.2.0.Final.jar
          - hibernate-jpa-2.0-api-1.0.1.Final.jar
          - javassist-3.15.0-GA.jar
          - jboss-logging-3.1.0.GA.jar
          - jboss-transaction-api_1.1_spec-1.0.0.Final.jar

      d) create a user libraries name hibernate in eclipse, and add all above jar into this user library.

      e) create a user libraries for the target jdbc driver, and add the jdbc jar into this user library
Creating Hibernate library

     f) add these 2 user libraries to the JPA project.
adding Hibernate library


    at this stage, all the Eclipse compilation errors should be gone.

2. configure persistence properties in persistence.xml.

    a) d-click to open persistence.xml in <JPA_Project>/src/META-INF

    b) in General tab, fill in Persistence provider by your choice of JPA implementation.
EclipseLink org.eclipse.persistence.jpa.PersistenceProvider
Hibernteorg.hibernate.ejb.HibernatePersistence

adding Persistence provider in General tab

    c) in Connection tab, choose Resource Local for Transaction type

    d) Fill in Driver, URL, User, Password or Populate from connection... to choose a configured database connection
Fill in connection details in Connection tab

   e) save persistence.xml


PREV Generating Entities Testing the JPA project NEXT


Done!!


No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...