无法将数据库状态与会话同步 [英] Could not synchronize database state with session

查看:111
本文介绍了无法将数据库状态与会话同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法将数据库状态与会话同步 org.hibernate.exception.GenericJDBCException:无法更新

Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: could not update

尝试更新数据库时出现此错误.我的数据库中没有定义唯一键,但是id字段已定义为主键.

I am getting this error while I try to update my database. There is no unique key defined in my database, but id field has been defined as Primary Key.

这是更新功能的代码:

public String updateAction(){
       Session session = null;
       try{
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            session =sessionFactory.openSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Medicine medicine = (Medicine) session.load(Medicine.class, id);
            medicine.setBrandName(brandName);
            session.update(medicine);
            tx.commit();
       }
       catch(Exception ex){
            ex.printStackTrace();
       }
       finally{
            session.close();
       }
       return "index";
    }

hibernate.config.xml:

hibernate.config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/medicine</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
    <property name="hibernate.jdbc.factory_class">org.hibernate.jdbc.NonBatchingBatcherFactory</property>
    <mapping resource="mediview/Medicine.hbm.xml"/>
  </session-factory>

</hibernate-configuration>

Medicine.java:

Medicine.java:

    public class Medicine  implements java.io.Serializable {

         private String id;
         private String brandName;

         public Medicine() {
         }

     public Medicine(String id) {
          this.id = id;
     }

     public Medicine(String id, String brandName) {
          this.id = id;
          this.brandName = brandName;
     }

     public String getId() {
          return this.id;
     }

     public void setId(String id) {
          this.id = id;
     }

     public String getBrandName() {
          return this.brandName;
     }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }
}

Medicine.hbm.xml:

Medicine.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 14 Aug, 2013 8:46:46 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="mediview.Medicine" table="medicine" catalog="medicine">
        <id name="id" type="string">
            <column name="ID" length="5" />
            <generator class="assigned" />
        </id>
        <property name="brandName" type="string">
            <column name="BrandName" length="30" />
        </property>
    </class>
</hibernate-mapping>

推荐答案

在更新之前刷新会话状态.

Do a refresh of the session state before you do the update.

Medicine medicine = (Medicine) session.load(Medicine.class, id);
session.refresh( medicine );

http://docs.jboss.org /hibernate/orm/4.0/devguide/zh-CN/html_single/#d0e1718

这篇关于无法将数据库状态与会话同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆