• Hibernate 异常汇总


     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>

    3。 No row with the given identifier exists: [cn.entity.Paper#1211]

    这个错误的出现是因为数据库中两张表的主键和外键不对称产生了脏数据造成的 去检查数据库数据

  • 相关阅读:
    LeetCode Subsets II
    LeetCode Rotate Image
    LeetCode Palidrome Number
    LeetCode Generate Parentheses
    LeetCode Maximum Subarray
    LeetCode Set Matrix Zeroes
    LeetCode Remove Nth Node From End of List
    Linux Loop设备 使用
    Linux 文件系统大小调整
    LeetCode N-Queens II
  • 原文地址:https://www.cnblogs.com/wanghongjie/p/4812020.html
Copyright © 2020-2023  润新知