Friday, October 28, 2011

Configuring your eclipse JEE6 project to use JPA (Step by Step guide)

Driver definition
The driver is vendor specific and it will allow the application to connect to the database.
To create a new driver definition for your app that follow this steps in eclipse:
         1- Select Window > Preferences from the main menu bar.
        2- Expand Data Management > Connectivity and select Driver Definitions.
        3- Click Add.                 
       4-In the Name/Type tab, indicate the version of Derby on which you want to create the driver definition.

    1. Select Derby Client JDBC Driver for the non embedded version
    2. Select Derby Embedded JDBC Driver for the embedded version
    3.  (Optional) Modify the Driver Name if a driver definition with this name already exists.  
      Location of the driver in windows systems is often: C:\jeeAplicationServer\glassfishv3/javadb/lib/derbyclient.jar
      5-In the JAR List tab, highlight the default derby.jar file and click Edit JAR/Zip to indicate the fully qualified  path of the Derby JAR file.

    1. Browse to the location of the ZIP file.
    2. Click Open.
6- (Optional) In the Properties tab, modify the default properties for the driver definition template.
Options in the Properties tab vary based on database server type. You can edit or enter a property in the Value box.

7- Click Ok

NOTE: THE driver.jar is for emdebed database while driverclient.jar is for normal database(driverclient should be used)

Start the derby database from the console
To be able to work with the derby database, you need first to start it. To do so, follow this steps:
1-Open the command line and place yourself at the glassfish server bin folder
2-And then enter the command: asadmin start-database

Create a new Connection Profile

Lets say that the connection profile is the configuration that will allow you interact with the database.

1-In the Data Source Explorer tab. Right click on Database Connections and select New…
2- Select the Derby profile
3- In the name field type the name of the database(Type the same as you typed in the Properties tab when you reated the driver definition)
5- Click next
6- In the Connection Details page, pick the driver you configured before and type the correct database. Select the save password option
7- Click on Test Connection to see all is correct. (You will receive a Ping Succeeded! message if all is ok)
8-Click Finish

Create a Connection Pool
In a nutshell, a connection pool allows the database recycle the already used connections. So here is explained how to create a connection pool for the previously created connection profile.

1- Once the glassfish server is started, go to http://localhost:4848
2- At the admin panel select connection Pools and click on New…
3- In general settings; give a name to the pool, The resource type is javax.sql.DataSource and the Database Vendor is Derby and click next
4-In the step 2 of 2. Let all by default, but just edit the setting below to look like this:

5- Click on finish

Adding JPA facelet and Referencing the JDBC resource from the application
At this point GlassFish knows about a database, but we still don’t reference it from the application. Let’s do that now. So here what we do is tell our application to use all the configurations that we configured before.

1- Enable JPA in the application by including the JPA facet:
Right click on Project->Properties->And search for project facets and select JPA
2- In the persistence.xml file you should add the name of the database(datasource) as says in the image:

Create entities
Now lets see how it works all we configured. Try creating an entity with a couple of fields and annotate it with the JPA annotations.

Remember that every entity must have an @Id attribute, if it doesn’t exist you should see something like this:
Create tables from entities
How to transform our entities into tables(Object Relational Mapping) follow this steps:

1- Right click on the project and select  Generate Tables from Entities
2- If this message appears in the screen click in Add a connection to JPA project…

3- The last missing piece is to add the connection name so all JPA features can work

4- Back at the schema selection dialog, you can now choose the schema that will be used for storing the tables.
By default USER schema will be used, so click cancel.
5-If a red error that says that the schema ‘USER’ cannot be found appears, just restart(build) the application.

Share with your friends