Data Persistence

Verified

Added on  2023/06/08

|17
|3328
|342
AI Summary
This report explains the role of data persistence in Java and evaluates various types of data persistence such as Java serialization, JDBC, JDO, and JPA. It also includes examples and advantages/disadvantages of each method.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Running Head: DATA PERSISTENCE
0
Data Persistence

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
DATA PERSISTENCE
1
Contents
Introduction...........................................................................................................................................2
Java Serialization...................................................................................................................................2
Advantage of data serialization.........................................................................................................6
Disadvantage of data serialization.....................................................................................................6
Java Database Connectivity (JDBC)........................................................................................................6
Advantage of JDBC.............................................................................................................................7
Disadvantage of JDBC........................................................................................................................8
Java Data Object (JDO)..........................................................................................................................8
Java Persistence APIs (JPA)..................................................................................................................10
Conclusion...........................................................................................................................................14
References...........................................................................................................................................15
Document Page
DATA PERSISTENCE
2
Introduction
Data persistence is a type of data which is used in data processing to denote information
of any data and it is infrequently accessed. In computer system persistence data is
defined as a data which outlives the method that produced it. There are many methods
through which data persistence in java can create such as serialization, JCA, JDBC, and
JPA. It is estimated that many data is persistence in any database and a relational
database is the best example of data persistence. Java data persistence involves
numbers, date, byte, bit, strings, arrays, image, and any Java object (Ang, and Wen,
2005). There are many applications of Java database that uses data persistence in their
model framework. There are many reasons to use data persistence in any database such
as it improves the productivity of database model, independent database, through this
database parameter of java can be handled, and it also avoids unnecessary queries. Java
is defined as an object-oriented language which is used to store data objects and it uses
data persistence process to persisting data in any model (Barmpis, and Kolovos, 2012).
This report is explaining about data persistence, use of data persistence in Java, and
different type of data persistence such as Java serialization, JDBC, JDO, and JPA. The
main objective of this report is to understand the role of data persistence in java and
evaluating various types of data persistence.
Java Serialization
Serialization in Java is defined as a process which is used to convert the state of any
object into a byte stream. Deserialization is inversely proportional to the serialization
process in which byte stream is converted into java object. Serialization is also used to
persistence data in the java language (Birk, Chao, and Chung, 2006). The byte stream
process produced an independent platform and serialization us one of the processes
which provide object serialization in java programming. To produce java object through
the serialization process we used the java.io.serializable method. There are some points
to remember when serialization method is used in java such as-
If any parent class has evaluated through serialization process then child class
does not require implementing.
In serialization process only non-static information or data are used.
Document Page
DATA PERSISTENCE
3
Related objects must be evaluated through the serialization process.
Transient data and static data are not saved through the serialization method.
Figure: Java Serialization process
(Source: Geeks for Geeks, 2018)
Example: Java code for serialization and deserialization
// of a Java object
import java.io.*;
class Emp implements Serializable {
private static final long serialversionUID =
129348938L;
transient int a;
static int b;
String name;
int age;
// Default constructor
public Emp(String name, int age, int a, int b)
{
this.name = name;
this.age = age;
this.a = a;
this.b = b;

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
DATA PERSISTENCE
4
}
}
public class SerialExample {
public static void printdata(Emp object1)
{
System.out.println("name = " + object1.name);
System.out.println("age = " + object1.age);
System.out.println("a = " + object1.a);
System.out.println("b = " + object1.b);
}
public static void main(String[] args)
{
Emp object = new Emp("ab", 20, 2, 1000);
String filename = "shubham.txt";
// Serialization
try {
// Saving of object in a file
FileOutputStream file = new FileOutputStream
(filename);
ObjectOutputStream out = new ObjectOutputStream
(file);
// Method for serialization of object
out.writeObject(object);
out.close();
file.close();
System.out.println("Object has been serialized\n"
+ "Data before Deserialization.");
printdata(object);
// value of static variable changed
object.b = 2000;
}
catch (IOException ex) {
Document Page
DATA PERSISTENCE
5
System.out.println("IOException is caught");
}
object = null;
// Deserialization
try {
// Reading the object from a file
FileInputStream file = new FileInputStream
(filename);
ObjectInputStream in = new ObjectInputStream
(file);
// Method for deserialization of object
object = (Emp)in.readObject();
in.close();
file.close();
System.out.println("Object has been deserialized\n"
+ "Data after Deserialization.");
printdata(object);
// System.out.println("z = " + object1.z);
}
catch (IOException ex) {
System.out.println("IOException is caught");
}
catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException" +
" is caught");
}
}
}
The output of java code-
The object has been serialized
Data before Deserialization
Document Page
DATA PERSISTENCE
6
Name = ab
Age = 20
a = 2
b = 1000
The object has been Deserialization
Data after Deserialization
Name = ab
Age = 20
a = 0
b = 2000
The advantage of data serialization
To provide persist the state of any Java object
To travel any java object with a network system
The disadvantage of data serialization
Very slow process
Not more efficient
Depend upon deployment size
Java Database Connectivity (JDBC)
JDBC is defined as an application programming interface for Java language and it
describes a platform to access database (Dominguez, Moellenhoff, and Chan, 2007). It is
also used for Java database model in java language and it is a part of java standard
platform (Keyse, et al., 2018). The main role of the application program interface is to
encode access data in SQL language. It is related to ODNC and it can be used to access
the database model by using ODBC process (Lawrence, Brandsberg, and Lee, 2017). It
provides a platform to write the java program for any database and it controls three
activities such as

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
DATA PERSISTENCE
7
Provide access to the database
Send queries and modify database model
Process-outcome acknowledged from the database
To connect the database with JDBC, it requires drivers system for each database model.
There are four types of drivers are used in JDBC such as type 1, type 2, type 3, and type
4. Type 1 and type 2 are used for programmers; type 3 and type 4 are utilized by
database vendors (Pater, and Moss, 2018).
Example: To connect JDBC with MySQL database
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
Document Page
DATA PERSISTENCE
8
Advantage of JDBC
Provide XML structure of any database
No content translation needed
Can be utilized for synchronous and Asynchronous systems
Also produce enterprise data
Does not require any installation system
Disadvantage of JDBC
Cannot modify
Produce random sequence (McCain, and Therrien, 2011).
Java Data Object (JDO)
JDO is defined as Java data objects which are used to developed data persistence for any
database model (Lafore, 2017). It is a platform to describe persistence data in any
database by using plain old java object and it also creates object persistence data for
java language. There are mainly three types of JDO classes such as persistence capable,
persistence aware, and normal. Capable type of class is used to provide persisted data
for any database, Aware is used to manipulate persistence data, and normal class does
not need any JDO metadata.
An object database is defined as a database model in this method data or information is
characterized in the form of an object which is used in the object-oriented program
(Maeda, 2012). ODBs are very different from the relational database model because
relational databases are table oriented but object databases are object-oriented. There
are main two components of this database such as state and behaviour. State considers
as a value and behaviour considered as operations, the operation defined into main two
parts such as implementation and signature (Roos, 2018).
Document Page
DATA PERSISTENCE
9
Figure: JDO Architecture
(Source: Oracle, 2010)
JDO helper is used to helping persistence data to provide an object for any database, and
persistence manager factory is a factory for persistence database. The persistence
manager is defined as primary JDO which is used for the application process, and
persistence capable is a class of JDO which is used to provide persistence data in the
java language (Schreter, SAP 2015). The transaction provides a platform to perform
persistence data in any database, and extent is a class of JDO which exist in the database.
A query is performed by the JDO vendor to determine persistence object, and fetch plan
control eager fetching and other data behaviour.
Example: This is the example of JDO which indicate how utilize execute with array in
JDO database.
/**
* Get all shipment for a specific order
*
* @param orderId Order Id

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
DATA PERSISTENCE
10
* @return -
*/
@Override
@Suppress Warnings("unchecked")
public List<IShipment> getShipmentsByOrderId(Long orderId, Persistence Manager
pm) {
Query query = null;
try {
query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM SHIPMENT WHERE
ORDERID = ?");
query.setClass(JDOUtils.getImplClass(IShipment.class));
List list = (List) query.executeWithArray(orderId);
List<IShipment> shipments = new ArrayList<>(list.size());
for (Object shipment : list) {
shipments.add((IShipment) shipment);
}
return shipments;
} catch (Exception e) {
xLogger.severe("Error while fetching shipments by order id: {0}", orderId, e);
} finally {
if (query != null) {
query.closeAll();
}
}
return null;
}
Java Persistence APIs (JPA)
JPA is defined as a java persistence API which is a specification of Java in terms of
accessing, managing data, and persisting data between Java objects and relational object
(Maeda, 2012). JPA is considered as ORM in java programming, and it is a specification
of Java language in terms of object and relational database. JPA provides plain old java
object which produces persistence data without using any class to evaluate data
Document Page
DATA PERSISTENCE
11
interface. JPA also define how Java classes map with a relational object database. At that
time the numbers of persistence, vendors have published for evaluation of JPA by users,
and industries. It also provides a platform to manage a relational database in java
language and it consists of four parts such as the persistence java API, java persistence
criteria, query language, and object mapping metadata.
ORM is an object-relational mapping which is a programming process to convert data
between incompatible type systems by the object-oriented program. In ORM, data
management mission directly acts on an object-oriented program which is a non-scalar
value. ORM provides a platform to utilize java object to represent relational database
and it uses two methods such as object-oriented, and relational oriented program.
There are many advantages of ORM such as
Hide all information on SQL from the object-oriented program
It is based on JDBC process
It does not require evaluating database
Automatic key generation
Fast process for development of any application
Figure: Architecture of JPA
(Source: Tutorial point, 2018)
Document Page
DATA PERSISTENCE
12
This figure shows the architecture of JPA and also indicates the core classes of JPA and
interface of java persistence API. Entity manager factory is used to produce manager
factory and also manage multiple entities of JPA factory. The entity manager is used to
managing persistence database, and an entity is used to store the database. Entity
transaction is providing a relationship with an entity manager and persistence is used
to obtain the entity manager factory. The query is used to providing persistence vendor
to implement relational object program.
Example: This is the example to create users entity in JPA
Package com.example.jpa.model;
Import javax.persistence.*;
Import javax.validation.constraints.NotNull;
Import javax.validation.constraints.Size;
Import javax.validation.constraints.Email;
Import java.io.Serializable;
@Entity
@Table (name = "users")
Public class User implements Serializable {
@Id
@Generated Value(strategy = GenerationType.IDENTITY)
Private Long id;
@Not Null
@Size (max = 65)
@Column (name = "first_name")
Private String firstName;

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
DATA PERSISTENCE
13
@Size (max = 65)
@Column (name = "last_name")
Private String lastName;
@NotNull
@Email
@Size(max = 100)
@Column(unique = true)
private String email;
@NotNull
@Size(max = 128)
private String password;
@OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "user")
private UserProfile userProfile;
// Hibernate requires a no-arg constructor
public User() {
}
public User(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
Document Page
DATA PERSISTENCE
14
this.password = password;
}
// Getters and Setters (Omitted for brevity)
}
Conclusion
The data persistence is a process to produce information of any database and it is
generally used in java language to produce database using oriented objective and
relational objective program. There are many types of data persistence method
available in java programmings such as Java serialization, java database connectivity,
java data object, and Java persistence JPA. Java serialization is used to convert data
object into the data stream and deserialization is the opposite process of serialization
that means this process converts data stream into java objects. The main role of JDBC in
Java programming is that it encodes access data into SQL database. This report
explained the role of data persistence in java programming and different types of data
persistence used in java. This report also researched how data convert into java objects
by using the object-oriented program. Java serialization should produce persistence
state of any Java object and JDBC should provide XML structure of any Java object.
Through data, persistence process programmer should produce objective oriented and
relational objected database and it should improve the overall efficiency of the
database. The main advantage of Java data object (JDO) is that it can produce data
persistence of any database by using plain old java object process.
Document Page
DATA PERSISTENCE
15
References
Ang, G.A.O. and Wen-xue, W.E.I., (2005) Application of Java data persistence with
Hibernate and Struts framework [J]. Computer Applications, 12, 2.
Barmpis, K. and Kolovos, D.S., (2012) Comparative analysis of data persistence
technologies for large-scale models. In Proceedings of the 2012 Extreme Modelling
Workshop, 12(1), pp. 33-38
Birk, P., Chao, C.Y. and Chung, H., (2006) Method and apparatus for handling custom
token propagation without Java serialization. U.S. Patent Application 10/882,118.
Dominguez Jr, F., Moellenhoff, D., and Chan, E., (2007) Java object cache server for
databases, U.S. Patent, 7, 209,929.
Geeks for Geeks (2018) Serialization and Deserialization in Java with Example. [online]
Available from: https://www.geeksforgeeks.org/serialization-in-java/ [Accessed
30/07/18].
Java T Point (2012) Java JDBC Tutorial. [online] Available from:
https://www.javatpoint.com/java-jdbc [Accessed 30/07/18].
Keyse, J., Treml, E.A., Huelsken, T., Barber, P.H., DeBoer, T., Kochzius, M., Nuryanto, A.,
Gardner, J.P., Liu, L.L., Penny, S. and Riginos, C., (2018) Historical divergences associated
with intermittent land bridges overshadow isolation by larval dispersal in co
distributed species of Tridacna giant clams. Journal of Biogeography, 45(4), pp.848-858.
Lafore, R., (2017) Data structures and algorithms in Java. Sams Publishing.
Lawrence, R., Brandsberg, E., and Lee, R., (2017) Next generation JDBC database drivers
for performance, transparent caching, load balancing, and scale-out. In Proceedings of
the Symposium on Applied Computing, 22, pp. 915-918.
Maeda, K., (2012) Performance evaluation of object serialization libraries in XML, JSON,
and binary formats. In Digital Information and Communication Technology and it's
Applications (DICTAP), 2012 Second International Conference on, 13(2), pp. 177-182.

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
DATA PERSISTENCE
16
McCain, B.S., and Therrien, A.L., (2011) Method and system for providing version
control of parameters in a command-based API using java serialization. U.S. Patent,
7,921,433.
Oracle (2010) JDO Architecture. [online] Available from:
https://docs.oracle.com/cd/E13189_01/kodo/docs303/jdo_overview_arch.html
[Accessed 30/07/18].
Pater, P. and Moss, K., (2018) Java database programming with JDBC. US: Coriolis.
Roos, R.M., (2018) Java Data Objects. US: Addison-Wesley.
Schreter, I., SAP S. E., (2015) Object storage and synchronization hooks for occasionally-
connected devices. U.S. Patent, 9,152,398.
Tutorial point (2018) JPA tutorial. [online] Available from:
https://www.tutorialspoint.com/jpa/ [Accessed 30/07/18].
1 out of 17
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]