Persisting Entity Classes Using XML in JPA - DZone Java
Persisting Entity Classes Using XML in JPA - DZone Java
Log In / Join
REFCARDZ RESEARCH WEBINARS | Agile AI Big Data Cloud Database DevOps Integration IoT Java Microservices Open Source Performance Security Web Dev
DZone > Java Zone > Persisting Entity Classes using XML in JPA
Introduction
Persistence is one of the major challenges for enterprise applications. JPA is the persistence standard from
Sun Microsystems. JPA supports two ways of con iguring persistence information- through annotations
and XML iles. This article discusses the XML based approach towards embedding persistence information
for the entity classes.
Instead of adding the con iguration details in the entity class in the form of annotations, we’ll add the
con iguration information in the orm.xml ile. Before we look into the code, let us discuss the orm.xml ile
in detail.
orm.xml
orm.xml ile contains all the con iguration details required for mapping a class to a relational database
table. These details include the primary key of the entity class and the various constraints/ rules to be
applied for the primary key. Other details about the entity class include the various attributes of the entity
class and columns to which the attributes should be mapped. We can also specify multiple constraints for
the same attribute. Other con igurable things include information about the various relationships the entity
may have with other entities (one-to-one, many-to-one, many-to-many or one-to-many), embedded
attributes, information about the version and transient attributes.
One application can have multiple entities. The con iguration details about all these entity classes,
embeddable classes go inside the same orm.xml ile. Name of this con iguration ile can be anything, not
mandatorily orm.xml. It should be placed in META-INF subdirectory along with the persistence.xml ile.
Persistence.xml ile should include the orm.xml ile. Please ind below sample orm.xml and
persistence.xml ile. The xml schema de initions are highlighted for both. Also, please note the entry
required in persistence.xml for including the orm.xml ile.
PFB the entries made in a sample orm.xml, which’ll persist employee instances to the database.
2. <description>One line string information about the entity classes in the application.
3. <package>speci ies the package of the classes listed within the sub elements and attributes of the
same mapping ile only.
4. <entity class= “entity.Employee” name= “Employee”>
1. classAttribute de ines the fully quali ied class name of the entity class
2. name: Attribute de ines the name of the entity
3. entity: tag can be repeated to embed mapping information for all the entity classes in the
application.
The Sub-Tags of entity tag are
<table>
maps the database table to which the entity class shall be persisted
<id-class>
overrides or creates a new id-class setting
<inheritance>
overrides or creates a new inheritance setting
<discriminator-value>
overrides or creates a new discriminator value setting, useful in Single_Table Inheritance strategy.
<discriminator-column>
overrides or creates a new discriminator column setting, used while con iguring Super class in
Single_Table inheritance strategy.
<sequence-generator>
A sequence-generator is unique by name
<table-generator>
A table-generator is unique by name
<named-query>
used to de ine named-query for the entity class. Can be repeated to de ine multiple named queries
for the entity class.
<named-native-query>
used to de ine native named query for the entity class.
<pre-persist>
creates or overrides a pre-persist setting i.e. the entity listener method to be invoked before persisting
the entity instance.
<post-persist>
creates or overrides a post-persist setting i.e. the entity listener method to be invoked after persisting
the entity instance.
<pre-remove>
creates or overrides a pre-remove setting i.e. the entity listener method to be invoked before
removing the entity instance.
<post-remove>
creates or overrides a post-remove setting i.e. the entity listener method to be invoked after removing
the entity instance.
<pre-update>
creates or overrides a pre-update setting i.e. the entity listener method to be invoked before updating
the entity instance.
<post-update>
creates or overrides a post-update setting i.e. the entity listener method to be invoked before
updating the entity instance.
<post-load>
creates or overrides a post-load setting i.e. the entity listener method to be invoked after loading the
entity instance state from database
<attributes>
de ines the attributes of the entity class which shall be persisted in the database table. The sub tags of
the attributes are:
1. <id>:- this tag de ines the id column of the entity class. Cannot be repeated for an entity. An
entity class can have only one Id attribute.
1. <generated-value> :-this is used to de ine the ID generation strategy to be used for the
primary key column.
2. <basic>:-this tag is used to map the entity columns to the columns in the database table. Should
be repeated to provide con igurations for all the attributes of the entity class.
1. <column>:- This tag is used to add the various column-level constraints on the entity
attributes. E.g. unique, insertable, updatable, length, precision etc.
Development Environment
We’ve used NetBeans IDE 6.5.1 for creating this JavaSE application for persistence through XML iles.
NetBeans has persistence support for JPA as well as Hibernate. So, we can choose either Hibernate or
Toplink Essentials as the Persistence Provider. Let’s name the application as “JPAEntity”. The name of
the entity class is “Employee” and is placed in the package “entity”. “EmpClient.java” is the client class.
The xml con iguration iles are put in META-INF sub directory. The directory structure of the
application is as follows:-
1. First JPA
META-INF
orm.xml
persistence.xml
entity
Employee.java
EmpClient.java
Step 2:Add the jar iles for the Persistence Provider (Toplink Essentials in our case),
JavaEE.jar and the database driver(DerbyClient.jar in our case) to the classpath.
Step 3: Add the entity class “Employee.java” and client class “EmpClient.java” in the package
"jpaentity"
This is the code of Employee.java
package jpaentity;
public class Employee {
private int empId;
private String empName;
private double empSalary;
public Employee() {
}
public Employee(int empId, String empName, double empSalary) {
this.empId = empId;
this.empName = empName;
this.empSalary = empSalary;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public double getEmpSalary() {
return empSalary;
}
public void setEmpSalary(double empSalary) {
this.empSalary = empSalary;
}
@Override
public String toString() {
return "Employee Id:="+empId+
+" Employee Name:="+empName+" Employee Salary:="+empSalary;
}
}//End of Employee.java
Step 4: Set up the database connection. Go to Services Tab in the NetBeans IDE and expand the
Databases node. Right click on JavaDB node and select Create Database. Give the database name,
username and password and click on OK. The database is created and ready to accept
connections.
Step 5: Add the persistence unit to the application. Also, add the orm.xml ile. The name of the
orm.xml ile can be changed. We have named it mapping.xml.
Following is the code of mapping.xml ile
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persist
version="1.0">
<description>My First JPA XML Application</description>
<package>entity</package>
<entity class="jpaentity.Employee" name="Employee">
<table name="EMPLOYEETABLE"/>
<attributes>
<id name="empId">
<generated-value strategy="TABLE"/>
</id>
<basic name="empName">
<column name="EMP_NAME" length="100"/>
</basic>
<basic name="empSalary">
</basic>
</attributes>
</entity>
</entity-mappings>
Advantages
1. No coupling between the metadata and the source code
2. Compatible with pre EJB3.0 development process
3. Support from IDEs like NetBeans, Eclipse etc
4. Easy to modify with the help of good editors.
Disadvantages
1. Complexity
2. Dif iculty in debugging in absence of editors
Wrap Up
This article helps in understanding entity con iguration with XML iles as an alternative to embedding
annotations in Java code for con iguring persistence details.
ABOUT US ADVERTISE
About DZone Developer Marketing Blog Let's be friends:
Send feedback Advertise with DZone
Careers +1 (919) 238-7100