Java Hibernate更新不起作用

Java Hibernate更新不起作用,java,hibernate,hibernate-mapping,Java,Hibernate,Hibernate Mapping,我的hibernate saveOrUpdate方法不起作用。 我不想使用load()或get()。是否可以更新记录。 我在dao层中的更新方法。我从main方法调用它。它不会抛出任何异常 public String updateEmp(EmployeeBean empBean) { Session session = null; try { session = sessionFactory.openSession(); if (null !=

我的hibernate saveOrUpdate方法不起作用。 我不想使用load()或get()。是否可以更新记录。 我在dao层中的更新方法。我从main方法调用它。它不会抛出任何异常

 public String updateEmp(EmployeeBean empBean) {
    Session session = null;
    try {
        session = sessionFactory.openSession();
        if (null != session) {
            session.beginTransaction();
            session.saveOrUpdate(empBean);
            System.out.println("Updated");
        } 

    } catch (Exception e) {
        session.getTransaction().rollback();
       e.printStackTrace();
    } finally {
        try {
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            session = null;
        }
    }
    return "updated";
}
我的主要方法是

 public static void main(String[] args) {
        Employee dao = new Employee();
        EmployeeBean empBean = new EmployeeBean();
        empBean.setEmpid("123"); //primary key in databse
        empBean.setFirstname("rahul");
        empBean.setLastname("sharma");
        dao.updateEmp(empBean);
    }

thanx prevance

将show_sql属性设置为true,以查看是否打印了一些sql

否则,您的会话可能为空,因此更新代码根本没有运行

打印下面的例外可能是您将看到

  try {
        session.getTransaction().commit();
        session.close();
    } catch (Exception e) {
        session = null;
    }

检查会话或尝试改用getCurrentSession()

否则,您的会话可能为空,因此更新代码根本没有运行

打印下面的例外可能是您将看到

  try {
        session.getTransaction().commit();
        session.close();
    } catch (Exception e) {
        session = null;
    }

检查会话或尝试改用getCurrentSession()

否则,您的会话可能为空,因此更新代码根本没有运行

打印下面的例外可能是您将看到

  try {
        session.getTransaction().commit();
        session.close();
    } catch (Exception e) {
        session = null;
    }

检查会话或尝试改用getCurrentSession()

否则,您的会话可能为空,因此更新代码根本没有运行

打印下面的例外可能是您将看到

  try {
        session.getTransaction().commit();
        session.close();
    } catch (Exception e) {
        session = null;
    }

检查会话或尝试改用getCurrentSession()。

提交事务的方式不正确。
它更喜欢在try块中提交。当try块{}中发生异常时,事务将在catch块中回滚。然后在finnaly块中发生另一个异常以提交()。

提交事务的方式不正确。
它更喜欢在try块中提交。当try块{}中发生异常时,事务将在catch块中回滚。然后在finnaly块中发生另一个异常以提交()。

提交事务的方式不正确。
它更喜欢在try块中提交。当try块{}中发生异常时,事务将在catch块中回滚。然后在finnaly块中发生另一个异常以提交()。

提交事务的方式不正确。
它更喜欢在try块中提交。当try块{}中发生异常时,事务将在catch块中回滚。然后在finnaly块中发生另一个异常以提交()。 我需要在EmployeeBean.hbm.xml中进行更改

<property name="email" type="string">
  <column name="EMAIL" length="60" not-null="true">
  </column>
</property>


not null=“true”>替换为not null=“false”>

经过一番研究,我找到了答案。 我需要在EmployeeBean.hbm.xml中进行更改

<property name="email" type="string">
  <column name="EMAIL" length="60" not-null="true">
  </column>
</property>


not null=“true”>替换为not null=“false”>

经过一番研究,我找到了答案。 我需要在EmployeeBean.hbm.xml中进行更改

<property name="email" type="string">
  <column name="EMAIL" length="60" not-null="true">
  </column>
</property>


not null=“true”>替换为not null=“false”>

经过一番研究,我找到了答案。 我需要在EmployeeBean.hbm.xml中进行更改

<property name="email" type="string">
  <column name="EMAIL" length="60" not-null="true">
  </column>
</property>



not null=“true”>替换为not null=“false”>

我们可以查看您的表格数据吗?它是如何“不工作”的?如果在最后一个catch块中添加e.printStackTrace(),而不是忽略异常,会发生什么?在数据库中,empId=123(它是主键)已经存在。我只想更新firstName和lastName。在finally catch e.printStacktrace()中,Db表中的其他列是phnNumber、email等打印以下org.hibernate.PropertyValueException:notnull属性引用空值或临时值:EmployeeBean。email@javababa:现在您有了答案:电子邮件不能为空。我们可以查看您的表格数据吗?它是如何“不工作”的?如果在最后一个catch块中添加e.printStackTrace(),而不是忽略异常,会发生什么?在数据库中,empId=123(它是主键)已经存在。我只想更新firstName和lastName。在finally catch e.printStacktrace()中,Db表中的其他列是phnNumber、email等打印以下org.hibernate.PropertyValueException:notnull属性引用空值或临时值:EmployeeBean。email@javababa:现在您有了答案:电子邮件不能为空。我们可以查看您的表格数据吗?它是如何“不工作”的?如果在最后一个catch块中添加e.printStackTrace(),而不是忽略异常,会发生什么?在数据库中,empId=123(它是主键)已经存在。我只想更新firstName和lastName。在finally catch e.printStacktrace()中,Db表中的其他列是phnNumber、email等打印以下org.hibernate.PropertyValueException:notnull属性引用空值或临时值:EmployeeBean。email@javababa:现在您有了答案:电子邮件不能为空。我们可以查看您的表格数据吗?它是如何“不工作”的?如果在最后一个catch块中添加e.printStackTrace(),而不是忽略异常,会发生什么?在数据库中,empId=123(它是主键)已经存在。我只想更新firstName和lastName。在finally catch e.printStacktrace()中,Db表中的其他列是phnNumber、email等打印以下org.hibernate.PropertyValueException:notnull属性引用空值或临时值:EmployeeBean。email@javababa:现在您有了答案:电子邮件不能为空。否。结束呼叫必须发生在finally块中。尝试{something();commit();}catch(e){rollback();}最后{close();}将会话设置为null只是useless@Gab我从问题中复制了代码,以提醒作者打印
e
。不,结束呼叫必须出现在finally块中。尝试{something();commit();}catch(e){rollback();}最后{close();}将会话设置为null只是useless@Gab我从问题中复制了代码,以提醒作者打印
e
。不,结束呼叫必须出现在finally块中。试试{something();commit();}catch(e){ro