Data storage is a very common and basic requirement of enterprise applications. Data storage ensures that the data collected from the user will be saved over time. The mechanism of saving data for the duration so as to make it available later is called persistency.
In Java, normally the persistency is of an object. Java can save an object state using the following ways:
- Object serialization
- Storing data in XML format
- Saving the data in a relational database
Using object serialization
The process by which an object is saved across the network is serialization. In Java, an object is saved in the file. So we can even say, saving an object to the file is serialization.
Disadvantages of using object serialization
The disadvantages of object serialization are as follows:
- Serialization adds resource overhead
- Serialization is slow
Storing data in XML
In the past few years, XML has emerged as a favorite choice to handle data in a file using markups. The good thing is, the developer can use their own tags to represent the data collection.
Disadvantages of storing data in XML
The disadvantages of storing data in XML are as follows:
- XML needs adequate processing applications
- XML is not for displaying in a browser but for holding data. Not all browsers have inbuilt support. Developers need to use XSLT (extensible stylesheet language) to present the data on browsers
Saving the data in a relational database
An object consists of data members to present its state. The relational database consists of a table structure which has a row and column structure. One row in a table is mapped to one object. Mapping is a technique that places an object's data members in one or more fields of a database table. The relational database uses an easy query writing mechanism to deal with the stored data. Over the years, the relational database has become the choice of Java developers for achieving persistency.
Advantages of saving data in a relational database
The advantages of saving data in a relational database are as follows:
- It supports easy mapping of a table in the database to object data members
- It has techniques such as primary key, timestamps and version numbers to update an object as and when required
- It provides mapping techniques that can support Java inheritance
- It supports relational mappings such as one to one, one to many, and many to many, which can be used to map Java collections and arrays