- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 331字
- 2021-07-23 16:55:00
Implementing the PersistenceManager class
We need to manage all processes of persistence and query data. For this, we need to create a class.
The PersistenceManager class is responsible for managing all persistence processes and query processes:
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class PersistenceManager {
private Set<StageManager> stateManagers;
@Inject @Transactional Transaction transaction;
@PostConstruct
public void init(){
stateManagers = new LinkedHashSet<StageManager>();
}
public Optional<Entity> persist ( Entity entity ) {
if ( entity instanceof Employee ){
stateManagers.add( new EmployeeStageManager( (Employee)
entity ) );
}
return Optional.ofNullable(entity);
}
public void begin() throws Exception {
if( !transaction.isOpened() ){
transaction.begin();
}
else{
throw new Exception( "Transaction already is opened" );
}
}
public Optional<Entity> load(Object id){
Entity entity = stateManagers.stream()
.filter( e-> e.getEntity().getId().equals( id ) )
.map( s->s.getEntity() )
.findFirst()
.orElseGet( ()-> new EmployeeStoreManager().load( id ) );
if( Optional.ofNullable(entity).isPresent()
&& stateManagers.stream().map( s->s.getEntity()
).collect( Collectors.toList() ).contains( entity ) )
stateManagers.add( new EmployeeStageManager( (Employee)
entity) );
return Optional.ofNullable(entity);
}
public void commit() throws Exception {
if( transaction.isOpened() ){
stateManagers.stream().forEach( s-> s.flush() );
transaction.commit();
}
else{
throw new Exception( "Transaction is not opened" );
}
}
public void rollback() throws Exception {
if( transaction.isOpened() ) {
stateManagers = new LinkedHashSet<StageManager>();
transaction.rollback();
}
else {
throw new Exception( "Transaction is not opened" );
}
}
}
In the preceding code, we have the PersistenceManager class. This class has the stateManagers attribute, which is a set of StateManager used to control the read and write of each object model. It also has the Transaction attribute, which is used to control the transaction lifecycle. This class also has the persist method, which is used to write the data represented by the object model; the begin method, used to begin the transaction; the load method, used to read an object model's data; the commit method, used to commit the transaction; and finally, the rollback method, which is used to roll back the transaction.
- Ansible權(quán)威指南
- 嵌入式應(yīng)用程序設(shè)計(jì)綜合教程(微課版)
- 嵌入式Linux應(yīng)用開(kāi)發(fā)菜鳥(niǎo)進(jìn)階
- RESS Essentials
- 奔跑吧 Linux內(nèi)核(入門(mén)篇)
- Microsoft Operations Management Suite Cookbook
- Android物聯(lián)網(wǎng)開(kāi)發(fā)細(xì)致入門(mén)與最佳實(shí)踐
- NetDevOps入門(mén)與實(shí)踐
- VMware Horizon View Essentials
- Linux系統(tǒng)最佳實(shí)踐工具:命令行技術(shù)
- Windows Server 2008組網(wǎng)技術(shù)與實(shí)訓(xùn)(第3版)
- 嵌入式微系統(tǒng)
- 應(yīng)急指揮信息系統(tǒng)設(shè)計(jì)
- Learning IBM Watson Analytics
- Windows Azure實(shí)戰(zhàn)