Setup
Let's start. First, Maven. We need to modify pom.xml to download the jars and resolve dependencies. We need:
- Eclipse Link.
- JPA 2.0 spec API (we'll get it from geronimo)
We have then eclipse link and JPA 2.0 dependency definition:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_2.0_spec</artifactId>
<version>1.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
And HSQL:
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
<type>jar</type>
</dependency>
You may need to define the eclipselink repository too:
<repository>
<id>EclipseLink Repo</id>
<url>http://www.eclipse.org/downloads/download.phpr=1&nf=1&file=/rt/eclipselink/maven.repo</url>
</repository>
Persistence.xml
Now, we need to write the persistence.xml that configures the database and persistence unit. We give the persistence unit the name "jpa".
<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="jpa">
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:hsqldb:file:\opt\db\testb;shutdown=true"/>
<property name="javax.persistence.jdbc.user" value="SA"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
The properties are explained below:
Name | Value | Explanation |
javax.persistence.jdbc.driver | org.hsqldb.jdbcDriver | The name of JDBC driver. Here, we use embedded HSQL, so we use org.hsqldb.jdbcDriver. |
javax.persistence.jdbc.url | jdbc:hsqldb:file:\opt\db\testb;shutdown=true | The URL to access. Here, we use file \opt\db\testb file, and we use it as embedded database. |
javax.persistence.jdbc.user | SA | The user id to access the database. SA is the default user id for HSQL DB. |
javax.persistence.jdbc.password | The password for the user SA |
Usually, we need to define the list of classes that are persisted by the JPA implementation. We leave it empty for now, since we haven't defined any classes yet.
Then, put the persistence.xml under resources directory (oh yes, don't forget to put it under META-INF):
Persistent class
Good. We're ready to define the class we want to persist. Basically, any Java classes would do. For this article, we take Flight as example. Flight class represents information about a flight, including the usual airline, flight number, departure and arrival airport codes, and departure and arrival times.
Here is the Flight.java code:
To make the class persistable, we annotate the class with @Entity and add an id that will be used as the identifier of the persisted object.
Is that all ? Almost. Just one more detail, because java.util.Date can be used to define date, time, and timestamp, we need to explicitly annotate properties of type date with its use, not surprisingly TemporalType.DATE, TemporalType.TIME, and TemporalType.TIMESTAMP.
For our example, we'll use the latter.
All right, here is the annotated class with id property:
Good, we have the persistable class. Now, we add this class to the persistence.xml
We have then:
<persistence-unit name="jpa">
<class>org.arizal.model.Flight</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:hsqldb:file:\opt\db\testb;shutdown=true"/>
<property name="javax.persistence.jdbc.user" value="SA"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
Database
Now we're ready to define the table in the database. We need to run the database admin of HSQL and then create a table using the following SQL command:
CREATE TABLE FLIGHT
(ID INTEGER NOT NULL PRIMARY KEY,
NUMBER VARCHAR(4) NOT NULL,
AIRLINE VARCHAR(3) NOT NULL,
DEPARTUREAIRPORT VARCHAR(3) NOT NULL,
ARRIVALAIRPORT VARCHAR(3) NOT NULL,
DEPARTURETIME DATETIME,
ARRIVALTIME DATETIME)
Good. Close the admin, and we're ready to code.
Code
The heart of the JPA is the class called EntityManager. The class is the one who hides all the relational database complexity from the code. We need then to instantiate it. The instantiation is done as follow:
We need the code to create Flight object as follow:
Nothing special.
Here is the code to persist three new Flights.
Check now the database:
Great! Now, what about update ? Here's the code to update flight with id = 2 (AF 1205). Let's say we modify the arrival airport to Lyon (LYS).
What about getting all Flight instances ? Hmm..., here we need to launch a query. JPA of course supports query: "SELECT f From Flight f" does the business.
Here it is:
OK. That's all for JPA. Of course there are lot of details need to be addressed. But, that's not bad as a start. Maybe I'll write on more subjects on this.
Good. We're ready to define the class we want to persist. Basically, any Java classes would do. For this article, we take Flight as example. Flight class represents information about a flight, including the usual airline, flight number, departure and arrival airport codes, and departure and arrival times.
Here is the Flight.java code:
package org.arizal.model; public class Flight { public void setNumber(String number) { this.airline = airline; return arrivalTime; |
Is that all ? Almost. Just one more detail, because java.util.Date can be used to define date, time, and timestamp, we need to explicitly annotate properties of type date with its use, not surprisingly TemporalType.DATE, TemporalType.TIME, and TemporalType.TIMESTAMP.
For our example, we'll use the latter.
All right, here is the annotated class with id property:
package org.arizal.model; private String airline; private String departureAirport; private String arrivalAirport; |
We have then:
<persistence-unit name="jpa">
<class>org.arizal.model.Flight</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:hsqldb:file:\opt\db\testb;shutdown=true"/>
<property name="javax.persistence.jdbc.user" value="SA"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
Database
Now we're ready to define the table in the database. We need to run the database admin of HSQL and then create a table using the following SQL command:
CREATE TABLE FLIGHT
(ID INTEGER NOT NULL PRIMARY KEY,
NUMBER VARCHAR(4) NOT NULL,
AIRLINE VARCHAR(3) NOT NULL,
DEPARTUREAIRPORT VARCHAR(3) NOT NULL,
ARRIVALAIRPORT VARCHAR(3) NOT NULL,
DEPARTURETIME DATETIME,
ARRIVALTIME DATETIME)
Good. Close the admin, and we're ready to code.
Code
The heart of the JPA is the class called EntityManager. The class is the one who hides all the relational database complexity from the code. We need then to instantiate it. The instantiation is done as follow:
EntityManagerFactory emf = |
We need the code to create Flight object as follow:
private static Flight createFlight( |
Nothing special.
Here is the code to persist three new Flights.
public static void main(String args[]) throws ParseException, SQLException { |
Check now the database:
Great! Now, what about update ? Here's the code to update flight with id = 2 (AF 1205). Let's say we modify the arrival airport to Lyon (LYS).
public static void main(String args[]) throws ParseException, SQLException { |
Here it is:
public static void main(String args[]) throws ParseException, SQLException { EntityManager em = emf.createEntityManager(); |
No comments:
Post a Comment