Tuesday February 09, 2010
TOTD #122: Creating a JPA Persistence Unit using NetBeans 6.8
Taking TOTD #121 forward, this blog explains how to create a JPA Persistence Unit for a MySQL sample database and package it as a library. This JAR file can then be easily included in other web applications.
Lets get started!






| Class | Field |
|---|---|
| sakila.SalesByFilmCategory | category |
| sakila.ActorInfo | actorId |
| sakila.FilmList | fid |
| sakila.CustomerList | id |
| sakila.NicerButSlowerFilmList | fid |
| sakila.StaffList | id |
| sakila.SalesByStore | store |
Right-click on the project and select "Clean & Build". This generates "dist/SakilaPU.jar" and the structure looks like:

This JAR file can now be included in any web application. The pre-built JAR file can also be downloaded here.
In order for this PU to be used in an application server (such as GlassFish) that is pre-configured with a JDBC resource, the "persistence.xml" needs to be changed to:
<?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="SakilaPU" transaction-type="JTA"> <jta-data-source>jdbc/sakila</jta-data-source> <properties/> </persistence-unit> </persistence>
The JDBC resource name is specified using <jta-data-source>.
The key items to note about this pre-built JAR:
This JAR can be installed to your local Maven repository as:
mvn install:install-file -Dfile=SakilaPU.jar -DgroupId=org.glassfish.samples -DartifactId=sakilapu -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
and then included in your "pom.xml" as:
<dependency> <groupId>org.glassfish.samples</groupId> <artifactId>sakilapu</artifactId> <version>1.0</version> <scope>compile</scope> </dependency>
Even though this blog uses a MySQL sample database, these steps can be easily followed for any other database such as Oracle or JavaDB.
Technorati: totd javaee glassfish v3 jpa eclipselink persistenceunit mysql sakila netbeans
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Today's Page Hits: 3039
Total # blog entries: 1082