Hibernate transaction.commit

时间:2013-12-14 16:42:53

标签: java hibernate transactions

我使用Hibernate 4.2.4版本和PostgreSQL 9.2。交易问题。 实体“学生”通过注释与实体“组”进行多对一映射。 当我运行这个片段时

StudentDAOImpl year = new StudentDAOImpl();
List<Students> stud = Collections.synchronizedList(new ArrayList<Students>());
List<Students> stud1 = Collections.synchronizedList(new ArrayList<Students>());

stud = year.getStudentsFromGroup(7);
stud1 = year.getStudentsFromGroup(7);
for (Students cat : stud) {
    System.out.println(cat.getName()+cat.getSurname());
    }
if( stud1.isEmpty()){ System.out.println("List is empty");}

我得到下一个结果

  Bill Murrey
  Ivan Ivanov
  List is empty

如果我发表评论

   session.getTransaction().commit(); 

我得到了正确的结果。为什么会这样?

 @Override
public List<beans.Students> getStudentsFromGroup(Integer id)
        throws SQLException {
    Session session = null;
    List<Students>  students = Collections.synchronizedList(new ArrayList<Students>());
    try {
           session = HibernateUtil.currentSession();
           session.beginTransaction();
           students = session.createQuery ("from Students s where s.groups.groupid = :var1").setParameter("var1", id).list();   
           for (Students a : students) {
                Hibernate.initialize(a.getGroups());
                Hibernate.initialize(a.getTypeEducation());
            }
           session.getTransaction().commit();
         } catch (Exception e) {
             log.info("Проблемы с сессией! " + e.getMessage());
         } finally {
           if (session != null && session.isOpen()) {
               HibernateUtil.closeSession();
           }
         }
         return students;
}

我的HibernateUtil calss

 public class HibernateUtil {
 private static final SessionFactory sessionFactory;
 @SuppressWarnings("rawtypes")
private static final ThreadLocal session = new ThreadLocal();

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
                     configuration.getProperties()).buildServiceRegistry();
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

 @SuppressWarnings("unchecked")
public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
 }

 @SuppressWarnings("unchecked")
public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null) {
            s.close();
        }
        System.out.println("Сессия успешно закрыта! ");
 }

}

0 个答案:

没有答案