0% found this document useful (0 votes)
108 views

Persisting Entity Classes Using XML in JPA - DZone Java

This document explains how to configure entity classes for persistence using XML files instead of annotations in JPA. It discusses setting up the environment by adding required JAR files to the classpath. The orm.xml file contains all the configuration details needed to map entity classes to database tables, including attributes, relationships, and metadata. The persistence.xml file references the orm.xml file. An example orm.xml file is provided that maps employee entity instances to a database table.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Persisting Entity Classes Using XML in JPA - DZone Java

This document explains how to configure entity classes for persistence using XML files instead of annotations in JPA. It discusses setting up the environment by adding required JAR files to the classpath. The orm.xml file contains all the configuration details needed to map entity classes to database tables, including attributes, relationships, and metadata. The persistence.xml file references the orm.xml file. An example orm.xml file is provided that maps employee entity instances to a database table.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Java Zone Over a million developers have joined DZone.

Log In / Join  
REFCARDZ RESEARCH WEBINARS | Agile AI Big Data Cloud Database DevOps Integration IoT Java Microservices Open Source Performance Security Web Dev

[NEW REFCARD] Drupal 9 Essentials: An Introduction to Core Features Read Now 

DZone > Java Zone > Persisting Entity Classes using XML in JPA

Persisting Entity Classes using XML in


JPA

by Anu Bakshi · Mar. 06, 11 · Java Zone · Not set Calling all Database
Experts!
 Like (1)  Comment (1)  Save  Tweet  119.3k Views
Please consider taking 3-4 minutes to share
your experience with database
Join the DZone community and get the full member experience. JOIN FOR FREE management — in your career and
organization. Your insight will help inform our
research and support devs world-wide in
their jobs and lifelong learning journeys!
Preface
This document explains the working of a JPA application purely based on XML con igurations. This 
explains how to create entity classes without using any annotations in Java classes. Entire con iguration is Take the Survey
done using xml iles.

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.

Setting up the environment


JPA is light-weight persistence model. It works for both JavaSE and JavaEE applications. All the required
class iles are present in the JavaEE.jar ile which should be added to the classpath. Along with the
JavaEE.jar ile, we need to add the Persistence Provider jar ile as well to the classpath. The provider can be
Toplink/ Hibernate/ App server speci ic persistence provider. In this application, we’ve used Toplink as
the persistence provider. So we need to add toplink-essentials.jar ile as well in the class path.

To summarize, the jar iles required are:


1.JavaEE.jar
2.ToplinkEssentials.jar (Replace with the jar ile for Persistence Provider, in case you want to use other
persistence provider)
3.derbyClient.jar (For JavaDB (Derby) Database, replace this with the jar ile of your database)

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.

<?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/persistence/orm_1_0.xsd"
version="1.0">
<description>My First JPA XML Application</description>
<package>entity</package>
<entity class="entity.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>

Different properties in orm.xml file


The various properties in the orm.xml in the order de ined in xml schema de inition are discussed below:

1. Root tag is <entity-mappings>


It contains the following four types of elements:
1. The persistence-unit-metadata element contains metadata for the entire persistence unit. It is
unde ined if this element occurs in multiple mapping iles within the same persistence unit.
2. The package, schema, catalog and access elements apply to all the entity, mapped-superclass and
embeddable elements de ined in the same ile in which they occur.
3. The sequence-generator, table-generator, named-query, named-native-query and sql-result-set-
mapping elements are global to the persistence unit.
4. The entity, mapped-superclass and embeddable elements each de ine the mapping information
for a managed persistent class. The mapping information contained in these elements may be
complete or it may be partial.

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.

3. <version>:- maps the Version attribute of the entity class.

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

Developing the Application

Step1:Start the NetBeans IDE. Create a new Java Application

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

This is the code of EmpClient.java


package jpaentity;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class EmpClient {
private EntityManager em;
private EntityManagerFactory emf;
public void initEmfAndEm()
{
emf=Persistence.createEntityManagerFactory("JPAEntityPU");
em=emf.createEntityManager();
}
public void cleanup()
{
em.close();
}
public void insertAndRetrieve()
{
System.out.println("-------------------Creating the Objects---------------------");
Employee empObj1=new Employee(1, "Anu", 1000.0);
Employee empObj2=new Employee(2, "Rahul", 1500.0);
System.out.println("-------------------Starting the transaction---------------------");
em.getTransaction().begin();
em.persist(empObj1);
em.persist(empObj2);
System.out.println("-------------------Committing the transaction---------------------");
em.getTransaction().commit();
System.out.println("-------------------Objects saved successfully--------------------");
System.out.println("*******************************************************************");
System.out.println("------------------- Reading Objects--------------------");
List emps=em.createQuery("select p from Employee p").getResultList();
for (Employee current:emps)
System.out.println(current);
System.out.println("-------------------Finished Reading Objects--------------------");
}
public static void main(String args[])
{
EmpClient myClient=new EmpClient();
System.out.println("-------------------Starting the Client---------------------");
myClient.initEmfAndEm();
myClient.insertAndRetrieve();
myClient.cleanup();
System.out.println("---------------Shutting down the Client---------------------");
}
}//End of EmpClient.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>

Add the following code to the persistence.xml ile


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.or
<persistence-unit name="FirstJPAPU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<mapping-file>\META-INF\orm.xml</mapping-file>
<class>jpaentity.Employee</class>
<properties>
<property name="toplink.jdbc.user" value="jpa"/>
<property name="toplink.jdbc.password" value="jpa"/>
<property name="toplink.jdbc.url" value="jdbc:derby://localhost:1527/JPAAssignments"/>
<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>

Steps for execution


1. Compile the source iles
2. Build the project
3. Start the Database Server
4. Execute the “EmpClient” class.
5. Connect to the database & verify the table has been created & data is also added in the table.

Advantages and Disadvantages of Using XML for Configuration

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.

Like This Article? Read More From DZone


DZone Article DZone Article
JPA Implementation Patterns: Lazy JPA Tutorial: Setting up Persistence
Loading Configuration for Java SE Environment

DZone Article Free DZone Refcard


Spring Tips: JPA [Video] Java 14

Topics: HIBERNATE , JAVA , JPA , XML

 Like (1)  Comment (1)  Save  Tweet  119.3k Views

Opinions expressed by DZone contributors are their own.

Java Partner Resources

ABOUT US ADVERTISE
About DZone Developer Marketing Blog Let's be friends:    
Send feedback Advertise with DZone
Careers +1 (919) 238-7100

CONTRIBUTE ON DZONE CONTACT US DZone.com is powered by


MVB Program 600 Park Offices Drive
Zone Leader Program Suite 150
Become a Contributor Research Triangle Park, NC 27709
Visit the Writers' Zone [email protected]
+1 (919) 678-0300
LEGAL
Terms of Service
Privacy Policy

You might also like