JPA 2 Employee Demo with Hibernate EclipseLink

Hibernate and EclipseLink both fully support JSR-317 (JPA 2.0), Hibernate have the largest community and is widely used/tested, EclipseLink are more light weight and feature at Service Data Objects(SDO) and Object-XML (JAXB), Object-JSON and integration with EIS. Both of them have features for which the other doesn’t have equivalent.

This document supply a JPA 2 Employee Demo with Hibernate and EclipseLink, the Demo source code can be found here. The Demo architecture looks like

architecture

Model

Model mainly contain one Employee entity, each Employee have one Address, have more phone number, it looks like

architecture

The whole Model Source code can be found here

Hibernate

Assume mysql database testdb be created, and mysql user jdv_user/jdv_pass have rights to operate the testdb.

The Hibernate I used in this document is 4.2.0.Final, I have import it in pom.xml

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>4.2.0.Final</version>
		</dependency>

The persistence.xml configured like

<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="org.jboss.demo.jpa.model"  transaction-type="RESOURCE_LOCAL">
   	  
      <class>org.jboss.demo.jpa.model.Address</class>
      <class>org.jboss.demo.jpa.model.Employee</class>
      <class>org.jboss.demo.jpa.model.EmploymentPeriod</class>
      <class>org.jboss.demo.jpa.model.PhoneNumber</class>
      <class>org.jboss.demo.jpa.model.Gender</class>
      
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>      
         <property name="hibernate.hbm2ddl.auto" value="create" />
         <property name="hibernate.show_sql" value="true" />
         
         <!-- mysql db connection -->
         
         <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
         <property name="hibernate.connection.username" value="jdv_user"/>
         <property name="hibernate.connection.password" value="jdv_pass"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testdb"/>

      </properties>
   </persistence-unit>
</persistence>

Either run EmployeeHibernate as Java Application, or run EmployeeHibernateTest as junit test, it will first add 2 employees to mysql, then quey DB via JPA NamedQuery, the output like

create 3 Employees

JPA NamedQuery Employee.findAll
[firstName=Emma, gender=Female, lastName=Brown, salary=300000.0]
[firstName=Madison, gender=Female, lastName=Jones, salary=300000.0]
[firstName=Mia, gender=Female, lastName=Johnson, salary=300000.0]

JPA NamedQuery Employee.findByName

JPA NamedQuery Employee.count
3

JPA NamedQuery Employee.address
[city=BJ, country=CNA, province=BJ, postalCode=100020, street=DDQ]
[city=BJ, country=CNA, province=BJ, postalCode=100020, street=DDQ]
[city=BJ, country=CNA, province=BJ, postalCode=100020, street=DDQ]

Assume mysql database testdb be created, and mysql user jdv_user/jdv_pass have rights to operate the testdb.

The EclipseLink I used in this document is 2.5.0, I have import it in pom.xml

		<dependency>
			<groupId>org.eclipse.persistence</groupId>
			<artifactId>eclipselink</artifactId>
			<version>2.5.0</version>
		</dependency>

The persistence.xml configured like

<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="org.jboss.demo.jpa.model"  transaction-type="RESOURCE_LOCAL">
   	  
      <class>org.jboss.demo.jpa.model.Address</class>
      <class>org.jboss.demo.jpa.model.Employee</class>
      <class>org.jboss.demo.jpa.model.EmploymentPeriod</class>
      <class>org.jboss.demo.jpa.model.PhoneNumber</class>
      <class>org.jboss.demo.jpa.model.Gender</class>
      
      <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testdb" />
      <property name="javax.persistence.jdbc.user" value="jdv_user" />
      <property name="javax.persistence.jdbc.password" value="jdv_pass" />

      <!-- EclipseLink should create the database schema automatically -->
      <property name="eclipselink.ddl-generation" value="create-tables" />
      <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>
      
   </persistence-unit>
</persistence>

Either run EmployeeEclipseLink, as Java Application, or run EmployeeEclipseLinkTest as junit test, it will first add 2 employees to mysql, then quey DB via JPA NamedQuery, the output like

create 3 Employees

JPA NamedQuery Employee.findAll
[firstName=Ethan, gender=Male, lastName=Johnson, salary=300000.0]
[firstName=Abigail, gender=Female, lastName=Rodriguez, salary=300000.0]
[firstName=Ethan, gender=Male, lastName=Wilson, salary=300000.0]

JPA NamedQuery Employee.findByName

JPA NamedQuery Employee.count
3

JPA NamedQuery Employee.address
[city=BJ, country=CNA, province=BJ, postalCode=100020, street=DDQ]
[city=BJ, country=CNA, province=BJ, postalCode=100020, street=DDQ]
[city=BJ, country=CNA, province=BJ, postalCode=100020, street=DDQ]