1 Public intupdateemp (emp emp) {2 Transaction TX;3Session session =sft.getsessionfafactory (). Opensession ();4 Try {5 //The thing that opens the session must be placed in a thing object in order to do the following or to report a null pointer exception6tx=session.begintransaction ();7 session.update (EMP);8 tx.commit ();9 return1;Ten}Catch(hibernateexception e) { One Tx.rollback (); A e.printstacktrace (); - return0; -}finally{ the sft.closesession (); -}
Can also be written like this
1 Public intaddemp (emp emp) {2Session session =sft.getsessionfafactory (). Opensession ();3 Try {4 session.begintransaction ();5 Session.save (EMP);6 //tx.commit ();7 session.gettransaction (). commit ();8 return1;9}Catch(hibernateexception e) {Ten session.gettransaction (). rollback (); One e.printstacktrace (); A return0; -}finally{ - sft.closesession (); the}
2 object references an unsaved transient instance
Modify Cascade= "Save-update"
1 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 have been committed
Hibernate is reported java.lang.NullPointerException for the commit method.