docs: serialized entity

This commit is contained in:
Ilkka Seppälä
2024-05-19 20:50:49 +03:00
parent ddf20b8262
commit d798a53b03
5 changed files with 150 additions and 164 deletions
+142 -160
View File
@@ -1,26 +1,43 @@
---
title: Serialized Entity Pattern
categories: Architectural
title: Serialized Entity
categories: Data access
language: en
tag:
- Data access
- Data access
- Data transfer
- Persistence
---
## Also known as
* Object Serialization
## Intent
To easily persist Java objects to the database.
Enable easy conversion of Java objects to and from a serialized format, allowing them to be easily stored and transferred.
## Explanation
Java serialization allow us to convert the object to a set of bytes. We can store these bytes into database as
BLOB(binary long objects) and read them at any time and reconstruct them into Java objects.
Real-world example
> An example of the Serialized Entity design pattern in the real world can be found in the process of saving and loading game state in video games. When a player decides to save their progress, the current state of the game, including the player's position, inventory, and achievements, is serialized into a file format such as JSON or binary. This file can then be stored on the player's device or on a cloud server. When the player wants to resume their game, the serialized data is deserialized back into the game's objects, allowing the player to continue from where they left off. This mechanism ensures that complex game states can be easily saved and restored, providing a seamless gaming experience.
In plain words
> The Serialized Entity design pattern enables the conversion of Java objects to and from a serialized format for easy storage and transfer.
Wikipedia says
> In computing, serialization is the process of translating a data structure or object state into a format that can be stored (e.g. files in secondary storage devices, data buffers in primary storage devices) or transmitted (e.g. data streams over computer networks) and reconstructed later (possibly in a different computer environment). When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of objects does not include any of their associated methods with which they were previously linked.
**Programmatic Example**
Walking through our customers example, here's the basic `Customer` entity.
The Serialized Entity design pattern is a way to easily persist Java objects to the database. It uses the Serializable interface and the DAO (Data Access Object) pattern. The pattern first uses Serializable to convert a Java object into a set of bytes, then it uses the DAO pattern to store this set of bytes as a BLOB (Binary Large OBject) in the database.
First, we have the `Country` class, which is a simple POJO (Plain Old Java Object) that represents the data that will be serialized and stored in the database. It implements the `Serializable` interface, which means it can be converted to a byte stream and restored from it.
```java
@Getter
@Setter
@EqualsAndHashCode
@@ -32,190 +49,155 @@ public class Country implements Serializable {
private String name;
private String continents;
private String language;
public static final long serialVersionUID = 7149851;
// Constructor ->
// getters and setters ->
}
```
Here is `CountrySchemaSql`, this class have method allow us to serialize `Country` object and insert it into the
database, also have a method that read serialized data from the database and deserialize it to `Country` object.
```java
@Slf4j
public class CountrySchemaSql {
public static final String CREATE_SCHEMA_SQL = "CREATE TABLE IF NOT EXISTS WORLD (ID INT PRIMARY KEY, COUNTRY BLOB)";
public static final String DELETE_SCHEMA_SQL = "DROP TABLE WORLD IF EXISTS";
private Country country;
private DataSource dataSource;
/**
* Public constructor.
*
* @param dataSource datasource
* @param country country
*/
public CountrySchemaSql(Country country, DataSource dataSource) {
this.country = new Country(
country.getCode(),
country.getName(),
country.getContinents(),
country.getLanguage()
);
this.dataSource = dataSource;
}
/**
* This method will serialize a Country object and store it to database.
* @return int type, if successfully insert a serialized object to database then return country code, else return -1.
* @throws IOException if any.
*/
public int insertCountry() throws IOException {
var sql = "INSERT INTO WORLD (ID, COUNTRY) VALUES (?, ?)";
try (var connection = dataSource.getConnection();
var preparedStatement = connection.prepareStatement(sql);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oss = new ObjectOutputStream(baos)) {
oss.writeObject(country);
oss.flush();
preparedStatement.setInt(1, country.getCode());
preparedStatement.setBlob(2, new ByteArrayInputStream(baos.toByteArray()));
preparedStatement.execute();
return country.getCode();
} catch (SQLException e) {
LOGGER.info("Exception thrown " + e.getMessage());
}
return -1;
}
/**
* This method will select a data item from database and deserialize it.
* @return int type, if successfully select and deserialized object from database then return country code,
* else return -1.
* @throws IOException if any.
* @throws ClassNotFoundException if any.
*/
public int selectCountry() throws IOException, ClassNotFoundException {
var sql = "SELECT ID, COUNTRY FROM WORLD WHERE ID = ?";
try (var connection = dataSource.getConnection();
var preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, country.getCode());
try (ResultSet rs = preparedStatement.executeQuery()) {
if (rs.next()) {
Blob countryBlob = rs.getBlob("country");
ByteArrayInputStream baos = new ByteArrayInputStream(countryBlob.getBytes(1, (int) countryBlob.length()));
ObjectInputStream ois = new ObjectInputStream(baos);
country = (Country) ois.readObject();
LOGGER.info("Country: " + country);
}
return rs.getInt("id");
}
} catch (SQLException e) {
LOGGER.info("Exception thrown " + e.getMessage());
}
return -1;
}
@Serial
private static final long serialVersionUID = 7149851;
}
```
This `App.java` will first delete a World table from the h2 database(if there is one) and create a new table called
`WORLD` to ensure we have a table we want.
It will then create two `Country` objects called `China` and `UnitedArabEmirates`, then create two `CountrySchemaSql`
objects and use objects `China` and `UnitedArabEmirates` as arguments.
Finally, call `SerializedChina.insertCountry()` and `serializedUnitedArabEmirates.insertCountry()` to serialize and
store them to database, and call `SerializedChina.selectCountry()` and `serializedUnitedArabEmirates.selectCountry()`
methods to read and deserialize data items to `Country` objects.
Next, we have the `CountryDao` interface, which defines the methods for persisting and retrieving `Country` objects from the database.
```java
public interface CountryDao {
int insertCountry() throws IOException;
int selectCountry() throws IOException, ClassNotFoundException;
}
```
The `CountrySchemaSql` class implements the `CountryDao` interface. It uses a `DataSource` to connect to the database and a `Country` object that it will serialize and store in the database.
```java
@Slf4j
public class App {
private static final String DB_URL = "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1";
public class CountrySchemaSql implements CountryDao {
public static final String CREATE_SCHEMA_SQL = "CREATE TABLE IF NOT EXISTS WORLD (ID INT PRIMARY KEY, COUNTRY BLOB)";
public static final String DELETE_SCHEMA_SQL = "DROP TABLE WORLD IF EXISTS";
private App() {
private Country country;
private DataSource dataSource;
public CountrySchemaSql(Country country, DataSource dataSource) {
this.country = new Country(
country.getCode(),
country.getName(),
country.getContinents(),
country.getLanguage()
);
this.dataSource = dataSource;
}
/**
* Program entry point.
* @param args command line args.
* @throws IOException if any
* @throws ClassNotFoundException if any
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
final var dataSource = createDataSource();
deleteSchema(dataSource);
createSchema(dataSource);
final var China = new Country(
86,
"China",
"Asia",
"Chinese"
);
final var UnitedArabEmirates = new Country(
971,
"United Arab Emirates",
"Asia",
"Arabic"
);
final var serializedChina = new CountrySchemaSql(China, dataSource);
final var serializedUnitedArabEmirates = new CountrySchemaSql(UnitedArabEmirates, dataSource);
serializedChina.insertCountry();
serializedUnitedArabEmirates.insertCountry();
serializedChina.selectCountry();
serializedUnitedArabEmirates.selectCountry();
}
private static void deleteSchema(DataSource dataSource) {
@Override
public int insertCountry() throws IOException {
var sql = "INSERT INTO WORLD (ID, COUNTRY) VALUES (?, ?)";
try (var connection = dataSource.getConnection();
var statement = connection.createStatement()) {
statement.execute(CountrySchemaSql.DELETE_SCHEMA_SQL);
var preparedStatement = connection.prepareStatement(sql);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oss = new ObjectOutputStream(baos)) {
oss.writeObject(country);
oss.flush();
preparedStatement.setInt(1, country.getCode());
preparedStatement.setBlob(2, new ByteArrayInputStream(baos.toByteArray()));
preparedStatement.execute();
return country.getCode();
} catch (SQLException e) {
LOGGER.info("Exception thrown " + e.getMessage());
}
return -1;
}
private static void createSchema(DataSource dataSource) {
@Override
public int selectCountry() throws IOException, ClassNotFoundException {
var sql = "SELECT ID, COUNTRY FROM WORLD WHERE ID = ?";
try (var connection = dataSource.getConnection();
var statement = connection.createStatement()) {
statement.execute(CountrySchemaSql.CREATE_SCHEMA_SQL);
var preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, country.getCode());
try (ResultSet rs = preparedStatement.executeQuery()) {
if (rs.next()) {
Blob countryBlob = rs.getBlob("country");
ByteArrayInputStream baos = new ByteArrayInputStream(countryBlob.getBytes(1, (int) countryBlob.length()));
ObjectInputStream ois = new ObjectInputStream(baos);
country = (Country) ois.readObject();
LOGGER.info("Country: " + country);
}
return rs.getInt("id");
}
} catch (SQLException e) {
LOGGER.info("Exception thrown " + e.getMessage());
}
}
private static DataSource createDataSource() {
var dataSource = new JdbcDataSource();
dataSource.setURL(DB_URL);
return dataSource;
return -1;
}
}
```
Finally, in the `App` class, we create `Country` objects and `CountrySchemaSql` objects. We then call the `insertCountry` method to serialize the `Country` objects and store them in the database. We also call the `selectCountry` method to retrieve the serialized `Country` objects from the database and deserialize them back into `Country` objects.
```java
public static void main(String[] args) throws IOException, ClassNotFoundException {
final var dataSource = createDataSource();
deleteSchema(dataSource);
createSchema(dataSource);
final var China = new Country(86, "China", "Asia", "Chinese");
final var UnitedArabEmirates = new Country(971, "United Arab Emirates", "Asia", "Arabic");
final var serializedChina = new CountrySchemaSql(China, dataSource);
final var serializedUnitedArabEmirates = new CountrySchemaSql(UnitedArabEmirates, dataSource);
serializedChina.insertCountry();
serializedUnitedArabEmirates.insertCountry();
serializedChina.selectCountry();
serializedUnitedArabEmirates.selectCountry();
}
```
This is a basic example of the Serialized Entity design pattern. It shows how to serialize Java objects, store them in a database, and then retrieve and deserialize them.
## Class diagram
![alt_text](./etc/class_diagram.urm.png "Serialized Entity")
![Serialized Entity](./etc/class_diagram.urm.png "Serialized Entity")
## Applicability
Use the Serialized Entity pattern when
* Use when you need to persist the state of an object or transfer objects between different tiers of an application.
* Useful in scenarios where objects need to be shared over a network or saved to a file.
* You want to easily persist Java objects to the database.
## Known Uses
## Related patterns
[Data Access Object](https://github.com/iluwatar/java-design-patterns/tree/master/dao)
* Java's built-in serialization mechanism using `Serializable` interface.
* Storing session data in web applications.
* Caching objects to improve performance.
* Transferring objects in distributed systems using RMI or other RPC mechanisms.
## Consequences
Benefits:
* Simplifies the process of saving and restoring object state.
* Facilitates object transfer across different parts of a system.
* Reduces boilerplate code for manual serialization and deserialization.
Trade-offs:
* Can introduce security risks if not handled properly (e.g., deserialization attacks).
* Serialized formats may not be easily readable or editable by humans.
* Changes to the class structure may break compatibility with previously serialized data.
## Related Patterns
* [Data Transfer Object (DTO)](https://java-design-patterns.com/patterns/data-transfer-object/): Used to encapsulate data and send it over the network. Often serialized for transmission.
* [Proxy](https://java-design-patterns.com/patterns/proxy/): Proxies can serialize requests to interact with remote objects.
* [Memento](https://java-design-patterns.com/patterns/memento/): Provides a way to capture and restore an object's state, often using serialization.
## Credits
* [J2EE Design Patterns by William Crawford, Jonathan Kaplan](https://www.oreilly.com/library/view/j2ee-design-patterns/0596004273/re21.html)
* [komminen](https://github.com/komminen/java-design-patterns) (His attempts of serialized-entity inspired me and learned a lot from his code)
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
* [Effective Java](https://amzn.to/4cGk2Jz)
* [J2EE Design Patterns](https://amzn.to/4dpzgmx)
* [Java Concurrency in Practice](https://amzn.to/4aRMruW)
@@ -23,6 +23,7 @@
* THE SOFTWARE.
*/
package com.iluwatar.serializedentity;
import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
@@ -44,6 +45,7 @@ public class Country implements Serializable {
private String name;
private String continents;
private String language;
public static final long serialVersionUID = 7149851;
@Serial
private static final long serialVersionUID = 7149851;
}
@@ -22,6 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* In an application the Data Access Object (DAO) is a part of Data access layer. It is an object
* that provides an interface to some type of persistence mechanism. By mapping application calls to
@@ -35,7 +35,6 @@ class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
* throws an exception.
*/
@@ -23,12 +23,14 @@
* THE SOFTWARE.
*/
package com.iluwatar.serializedentity;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
public class CountryTest {
@Test
@@ -79,7 +81,7 @@ public class CountryTest {
objectOutputStream.writeObject(country);
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("Error occurred: ", e);
}
// De-serialize Country
@@ -97,7 +99,7 @@ public class CountryTest {
assertEquals(China, country);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error occurred: ", e);
}
}
}