标签:
1 public int updateEmp(Emp emp) { 2 Transaction tx; 3 Session session = Sft.getSessionfaFactory().openSession(); 4 try { 5 //session打开旳事物必须放在事物对象 中以便下面操作否则报空指针异常 6 tx=session.beginTransaction(); 7 session.update(emp); 8 tx.commit(); 9 return 1; 10 } catch (HibernateException e) { 11 tx.rollback(); 12 e.printStackTrace(); 13 return 0; 14 } finally{ 15 Sft.closeSession(); 16 }
也可以这样写
1 public int addEmp(Emp emp) { 2 Session session = Sft.getSessionfaFactory().openSession(); 3 try { 4 session.beginTransaction(); 5 session.save(emp); 6 //tx.commit(); 7 session.getTransaction().commit(); 8 return 1; 9 } catch (HibernateException e) { 10 session.getTransaction().rollback(); 11 e.printStackTrace(); 12 return 0; 13 } finally{ 14 Sft.closeSession(); 15 }
2 object references an unsaved transient instance
修改cascade="save-update"
1 <many-to-one name="dept" class="cn.entity.Dept" lazy="false" cascade="save-update" 2 column="dno" /> 3 4 5 6 7 8 <set name="emps" lazy="false" cascade="save-update" >
<key column="dno"/>
<one-to-many class="cn.entity.Emp"/>
</set>
3Cannot forward after response has been committed
Hibernate对于commit方法处报 java.lang.NullPointerException 。
标签:
原文地址:http://www.cnblogs.com/wanghongjie/p/4812020.html