Headline

Object-Relational mapping for Java with Technology:JPA

Motivation

Object-Relational mapping or persistence on the Java platform is exercised using Technology:JPA. The primary data model for companies is a straightforward object model for POJOs. A mapping is defined in the object models as Java-Annotations.

Illustration

Consider the following sketch of the class for departments:

@Entity
public class Department {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	private String name;
	
	@OneToOne(cascade=CascadeType.ALL)
	private Employee manager;
	
	@OneToMany(cascade=CascadeType.ALL)
	private List<Department> subdepts = new LinkedList<Department>();
	
	@OneToMany(cascade=CascadeType.ALL)
	private List<Employee> employees = new LinkedList<Employee>();

	public int getId() { ... }
	public void setId(int id) { ... }
	public String getName() { ... }
	public void setName(String name) { ... }
	public Employee getManager() { ... }
	public void setManager(Employee manager) { ... }
	public List<Department> getSubdepts() { ... }
	public List<Employee> getEmployees() { ... }
}

Each persistent class must provide a property (i.e., a getter and a setter) for an id that can serve as primary key in the database. Properties of collection types (in fact, list types) proxy for one-to-many relationships. The id and the relationsship must have an appropriate annotation. Other than that, a persistent class is not much different from a regular POJO-like class. The Department class is an Entity and is directly mapped to the DEPARTMENT table. All properties of the class are associated with the table. That is, the id property is directly mapped to a primary key column ID of the DEPARTMENT table. Also, the name property is mapped to a column of just that name---applying default mapping rules between SQL types and Java types. Further, the employees (one-to-many) property is mapped to a new table DEPARTMENT_EMPLOYEE with the colums DEPARTMENT_ID and EMPLOYEE_ID; likewise for sub-departments. The manager (one-to-one) property is mapped with a foreign key MANAGER_ID of the EMPLOYEE table.

The EntityManager is used to communicate with the database:

EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("jpa");
em = emFactory.createEntityManager();
The name jpa is defined in the JPA configuration.

Objects must be persist as follows:

	public static void persistCompany(EntityManager em, Company c){
		if(!em.getTransaction().isActive())
			em.getTransaction().begin();
		em.persist(c);
		em.getTransaction().commit();
	}

Persistent objects are brought back to life as follows:

	public static Company loadCompany(EntityManager em, String name) {
		if(!em.getTransaction().isActive())
			em.getTransaction().begin();
		Company c = 
				(Company) em.createQuery("Select c from Company c where c.name = :name ", Company.class)
				.setParameter("name", name)
				.getSingleResult();
		return c;
	}

Finally, consider the JPA configuration:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
	<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<class>org.softlang.company.model.Company</class>
		<class>org.softlang.company.model.Department</class>
		<class>org.softlang.company.model.Employee</class>
		<properties>
		   <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC"/>
		   <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:data/jpa.db"/>
		   <property name="eclipselink.logging.level" value="ALL"/>
		   <property name="eclipselink.ddl-generation" value="create-tables"/>
		</properties>
	</persistence-unit>
</persistence>

This configuration helps the runtime to connect to the right database, to find all mapping files of interest, and to define some essential settings.

Architecture

Package org.softlang.company.model hosts the java object model for Feature:Hierarchical company and the O/R-mapping files. Package org.softlang.company.features provides functionality for Feature:Total and Feature:Cut as well as boilerplate code for Feature:Persistence. The JPA configuration file is located in src/META-INF. The SQLite database file are located in the data dir.

Build the project

The project should built fine within Eclipse without additional efforts.

Metadata


Hakan Aksu edited this article at Wed, 10 May 2017 23:01:02 +0200
Compare revisions Compare revisions

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.