Last active
May 3, 2020 16:03
-
-
Save acdcjunior/94363ea5cdbf4cae41c7 to your computer and use it in GitHub Desktop.
UnitOrWork + DAO + EntityManager programatic transaction handling suggestion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: This is not a by-the-book implementation of the UnitOfWork pattern. If you don't feel it is | |
// OK, then you can call this class Transaction or anything like that | |
public class UnitOfWork { | |
// static reference to entityManagerFactory | |
public static UnitOfWork createUnitOfWork() { | |
EntityManager entityManager = entityManagerFactory.createEntityManager(); | |
return new UnitOfWork(entityManager); | |
} | |
private final EntityManager entityManager; | |
private UnitOfWork(EntityManager entityManager) { | |
this.entityManager = entityManager; | |
} | |
public void begin() { | |
entityManager.getTransaction().begin(); | |
} | |
public void rollback() { | |
// for more robust support, this should be wrapped in a try/catch/finally block | |
entityManager.getTransaction().rollback(); | |
entityManager.close(); | |
} | |
public void commit() { | |
// for more robust support, this should be wrapped in a try/catch/finally block | |
entityManager.getTransaction().commit(); | |
entityManager.close(); | |
} | |
public EntityManager entityManager() { | |
return this.entityManager; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserBLImpl implements UserBL { | |
// ... | |
private void assignRightToUser(int userId, int rightId) throws SomeAppException{ | |
UnitOfWork unitOfWork = UnitOfWork.createUnitOfWork(); | |
UserDAO userDAO = new UserDAO(unitOfWork); | |
RightDAO rightDAO = new RightDAO(unitOfWork); | |
List<User> johns = userDAO.findUsersByName("John"); | |
try { | |
unitOfWork.begin(); | |
// use userDAO and rightDAO at will... | |
johns.get(0).setAge(123); | |
userDAO.save(johns.get(0)); | |
unitOfWork.commit(); | |
} catch(SomeJpAException e) { | |
unitOfWork.rollback(); | |
throw new SomeAppException(some message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserDAO { | |
private final EntityManager entityManager; | |
public UserDAO(UnitOfWork unitOfWork) { | |
this.entityManager = unitOfWork.entityManager(); | |
} | |
public User findUserById(int id) { | |
return this.entityManager.find(User.class, id); | |
} | |
// other find methods... such as | |
public List<User> findUsersByName(String name) { | |
TypedQuery<User> query = this.entityManager.createQuery("SELECT u FROM User u WHERE BY u.name = :name", User.class); | |
query.setParameter("name", name); | |
return query.getResultList(); | |
} | |
public void save(User user) { | |
if (user.getId() == null) { | |
this.entityManager.persist(user); | |
} else { | |
this.entityManager.merge(user); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment