Chapter 10: JPA Integration"

From Documentation
Line 104: Line 104:
 
</persistence>
 
</persistence>
 
</source>
 
</source>
 +
 +
<!--
 +
== Deployment Descriptor ==
 +
You don't have to add any special configuration for JPA to work. Here we add a optional <tt>OpernEntityMangerInViewFilter</tt> which can resolve a lazy-fetching issue in one-to-many mapping. In our example, there is no such issue
 +
 +
'''Extracted from web.xml'''
 +
<source lang='xml' high='14'>
 +
...
 +
<!-- Spring configuration -->
 +
<!-- Initialize spring context -->
 +
<listener>
 +
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 +
</listener>
 +
<!-- Enable webapp Scopes-->
 +
<listener>
 +
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
 +
</listener>
 +
 +
<filter>
 +
<filter-name>OpenEntityManagerInViewFilter</filter-name>
 +
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
 +
</filter>
 +
<filter-mapping>
 +
<filter-name>OpenEntityManagerInViewFilter</filter-name>
 +
<url-pattern>/*</url-pattern>
 +
</filter-mapping>
 +
...
 +
</source>
 +
 +
-->
  
 
== Entity Annotation ==
 
== Entity Annotation ==

Revision as of 10:14, 5 February 2013

Overview

In previous chapters, we mimic a database with a static list as follows:

public class UserInfoServiceImpl implements UserInfoService,Serializable{

	
	static protected List<User> userList = new ArrayList<User>();  
	static{
		userList.add(new User("anonymous","1234","Anonymous","[email protected]"));
		userList.add(new User("admin","1234","Admin","[email protected]"));
		userList.add(new User("zkoss","1234","ZKOSS","[email protected]"));
	}
	
	/** synchronized is just because we use static userList in this demo to prevent concurrent access **/
	public synchronized User findUser(String account){
		int s = userList.size();
		for(int i=0;i<s;i++){
			User u = userList.get(i);
			if(account.equals(u.getAccount())){
				return User.clone(u);
			}
		}
		return null;
	}
	
...
}

Java Persistence API (JPA) is a POJO-based persistence specification. It offers object-relational mapping solution to enterprise Java applications. In this chapter, we don't create new applications but re-write data persistence part based on chapter 9 with JPA. We will create a simple database with HSQL and implement a persistence layer in DAO (Data Access Object) pattern to encapsulate all database related operations. We also have to annotate all entity classes that will be stored in the database with JPA annotations. To make the example close to a real application, we keep the Spring framework and demonstrate how to integrate Spring with JPA.

Configuration

Maven

For using database, JPA, and integration of JPA and Spring, we should add following dependencies based on chapter 9's configuration: (The reason we add spring-web and cblib is explained in chapter 9.)


	<properties>
		<zk.version>6.5.1</zk.version>
		<maven.build.timestamp.format>yyyy-MM-dd</maven.build.timestamp.format>
		<packname>-${project.version}-FL-${maven.build.timestamp}</packname>
		<spring.version>3.1.2.RELEASE</spring.version>
	</properties>

...
		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency>
		<!-- JPA(Hibernate) and HSQL dependencies -->
		<dependency>
	  		<groupId>org.hibernate</groupId>
	  		<artifactId>hibernate-entitymanager</artifactId>
	  		<version>4.0.0.Final</version>
	  	</dependency>
		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>2.2.6</version>
		</dependency>
...
  • Line 5, 16~17: Spring provides a module to integrate several ORM (Object Relation Mapping) frameworks, integrating JPA requires this dependency.
  • Line 27~28: There are various implementations of JPA specification, we choose Hibernate's one which is the most popular.
  • Line 32~33: For using HSQL, we should add its JDBC driver.


Persistence Unit Configuration

We should describe persistence unit configuration in an XML file called persistence.xml and we need to specify name, transaction-type, and properties. The properties are used by persistence provider (Hibernate) to establish database connection and setup vendor specific configurations.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

	<persistence-unit name="myapp" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
			<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
			<property name="hibernate.connection.username" value="sa" />
			<property name="hibernate.connection.password" value="" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.connection.url" value="jdbc:hsqldb:file:data/store" />
			<property name="hibernate.hbm2ddl.auto" value="create" />
		</properties>
	</persistence-unit>
</persistence>

<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener>

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

</listener>

<filter> <filter-name>OpenEntityManagerInViewFilter</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenEntityManagerInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ... </source>

-->

Entity Annotation

Before storing objects into a database, we should specify OR (object-relation) mapping with meta data.

Todo class used in todo-list management

@Entity
@Table(name="apptodo")
public class Todo implements Serializable, Cloneable {


	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	Integer id;
	
	boolean complete;
	
	@Column(nullable=false,length=128)
	String subject;
	
	@Column(nullable=false,length=128)
	@Enumerated(EnumType.ORDINAL)
	Priority priority;
	
	@Temporal(TemporalType.TIMESTAMP)
	Date date;
	
	String description;

	//omit getter, setter, and other methods

}

User class used in profile form

@Entity
@Table(name="appuser")
public class User implements Serializable,Cloneable {

	
	@Id
	@Column(nullable=false,length=32)
	String account;
	
	@Column(nullable=false,length=128)
	String fullName;
	
	@Column(nullable=false,length=255)
	String password;
	
	@Column(nullable=false,length=255)
	String email;
	
	@Temporal(TemporalType.TIMESTAMP)
	Date birthday;

	//omit getter, setter, and other methods
}

Spring Beans Configuration

DAO Implementation