Answer the question
In order to leave comments, you need to log in
Am I working correctly with Hibernate sessions and transactions?
Good afternoon, I'm writing a small application using Hibernate + servlet, everything works, and since I'm on the way to learning Hibernate, the question arose Do I process the Session and transactions correctly?
A little about the code structure
There is a HibernateUtil class where the Sessionfactory is built.
There is an Abstract class where CRUD operations are declared, from it the DAOUser class that implements these methods is inherited + there is an interface in which there are methods (well, supposedly sharpened to work only with the USER class) and the DAOUser class implements them.
B in the DAOUser class, I implement CRUD methods, so tell me if I process them correctly and if there are errors in the structure or any others, I will be glad to hear how it is better or more correct to do it. If there are articles on this subject, this is also welcome!
I read that each operation should be processed only in a separate session, so I did it, but there is also a question: Did I implement an anti-pattern in this way?
DAOUser class:
public class DaoUser extends AbstractDAO<UserE, Integer> implements DAOUserInterface{
private SessionUtil sessionUtil;
public DaoUser(){
}
@Override
public List<UserE> getAll() {
List<UserE> users = new ArrayList<>();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try {
Query query = session.createSQLQuery("SELECT * FROM test.users").addEntity(UserE.class);
users = query.list();
tx.commit();
}catch (HibernateException e){
tx.rollback();
e.printStackTrace();
System.out.println("Error in method getAll " + e.getMessage());
return users;
}finally {
System.out.println("Sesion close getAll - in class USER");
session.close();
}
return users;
}
@Override
public UserE update(UserE entity) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try{
session.update(entity);
tx.commit();
}catch(HibernateException e){
tx.rollback();
e.printStackTrace();
System.out.println("Error in method update " + e.getMessage());
return null;
}finally {
System.out.println("Sesion close update - in class USER");
session.close();
}
return entity;
}
@Override
public UserE getEntityById(Integer id) {
UserE userE = new UserE();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try {
// tx = session.beginTransaction();
userE = session.load(UserE.class, id);
if (userE != null){
System.out.println("it is a getEntityById " + userE.toString());
}
tx.commit();
}catch (HibernateException e){
tx.rollback();
e.printStackTrace();
System.out.println("Error in method getEntityById " + e.getMessage());
}finally {
System.out.println("Sesion close getEntityById - in class USER");
session.close();
}
return userE;
}
@Override
public boolean delete(Integer id) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try{
UserE user = session.load(UserE.class, id);
if(user != null){
session.delete(user);
}
tx.commit();
}catch(HibernateException e){
tx.rollback();
e.printStackTrace();
System.out.println("Error in method delete " + e.getMessage());
return false;
}finally {
System.out.println("Sesion close delete - in class USER");
session.close();
}
return true;
}
@Override
public boolean create(UserE entity) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try {
session.save(entity);
// session.flush();
tx.commit();
}catch (HibernateException e){
tx.rollback();
e.printStackTrace();
System.out.println("Error in method create " + e.getMessage());
return false;
}finally {
System.out.println("Sesion close create - in class USER");
session.close();
}
return true;
}
@Override
public UserE findUserByNickName(String nickname) {
UserE userE = new UserE();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try {
userE = (UserE) session.createCriteria(UserE.class).add(Restrictions.eq("nickname", nickname)).uniqueResult();
if(userE != null) {
System.out.println(" IT IS FROM findUserByNickName = " +userE.toString());
}
tx.commit();
}catch (HibernateException e){
tx.rollback();
e.printStackTrace();
System.out.println("Error in method findUserByNickName " + e.getMessage());
}finally {
System.out.println("Sesion close findUserByNickName - in class USER");
session.close();
}
return userE;
}
@Override
public UserE findConfirmKey(String key) {
UserE userE = new UserE();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try {
userE = (UserE) session.createCriteria(UserE.class).add(Restrictions.eq("key", key)).uniqueResult();
if(userE != null) {
System.out.println(userE.toString());
}
tx.commit();
}catch (HibernateException e){
tx.rollback();
e.printStackTrace();
System.out.println("Error in method confirmKey " + e.getMessage());
}finally {
System.out.println("Sesion close findConfirmKey - in class USER");
session.close();
}
return userE;
}
}
Answer the question
In order to leave comments, you need to log in
Wow, so much code... Sessions recommended. put hibernate in servlet-filter, open it here, and close it right there.
Your call stack will be as follows
filter ->
-> servler
hibenate
<- servlet
filter
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question