, 우리는 DB와 함께 뭔가를 할 필요가있을 때 우리가 필요 1. 세션 2. 거래를 시작 3. 마침 거래 4. 닫기 세션세션 및 트랜잭션
- 예를 들어
나는 학생 목록을 얻으려면 :
public static List<Student> getStudentList() { List<Student> l = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { String hql = "from Student"; Query query = session.createQuery(hql); l = query.list(); } catch (HibernateException ex) { //Log the exception System.err.println(ex); } finally { session.close(); } return l; }
학생 삽입을
public static boolean addStudent(Student s) { Session session = HibernateUtil.getSessionFactory().openSession(); if (... /* check if student is already exists*/) { return false; } Transaction transaction = null; try { transaction = session.beginTransaction(); session.save(s); transaction.commit(); } catch (HibernateException ex) { //Log the exception transaction.rollback(); System.err.println(ex); } finally { session.close(); } return true; }
getStudentList()에 트랜잭션이없는 이유는 무엇입니까? 미리 감사하십시오