When a business transaction is completed, all the the updates are sent as one big unit of work to be persisted
in one go to minimize database round-trips.
## Explanation
Real world example
> We have a database containing student information. Administrators all over the country are constantly updating this information and it causes high load on the database server. To make the load more manageable we apply to Unit of Work pattern to send many small updates in batches.
In plain words
> Unit of Work merges many small database updates in single batch to optimize the number of round-trips.
> Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems.
**Programmatic Example**
Here's the `Student` entity that is being persisted to the database.
```java
public class Student {
private final Integer id;
private final String name;
private final String address;
public Student(Integer id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public Integer getId() {
return id;
}
public String getAddress() {
return address;
}
}
```
The essence of the implementation is the `StudentRepository` implementing the Unit of Work pattern. It maintains a map
of database operations (`context`) that need to be done and when `commit` is called it applies them in single batch.
```java
public interface IUnitOfWork<T> {
String INSERT = "INSERT";
String DELETE = "DELETE";
String MODIFY = "MODIFY";
void registerNew(T entity);
void registerModified(T entity);
void registerDeleted(T entity);
void commit();
}
public class StudentRepository implements IUnitOfWork<Student> {
private static final Logger LOGGER = LoggerFactory.getLogger(StudentRepository.class);