如何解决" javax.persistence.OptimisticLockException:"在休眠?

时间:2017-05-27 09:16:47

标签: java hibernate exception

错误:

May 27, 2017 2:39:48 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
May 27, 2017 2:39:48 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 27, 2017 2:39:48 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
May 27, 2017 2:39:49 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3305/db]
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
May 27, 2017 2:39:52 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
May 27, 2017 2:39:54 PM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1]
javax.persistence.OptimisticLockException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

代码:

pojo class


public class Testpojo {


    private int number;
     private String name;

    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}



main class

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;



public class TestMain {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Testpojo tp=new Testpojo();
        tp.setNumber(3);
        tp.setName("MEHUL1");

        try{

        SessionFactory ses=new Configuration().configure().buildSessionFactory();
        Session s1=ses.openSession();
        Transaction tr=s1.beginTransaction();
        s1.update(tp);
        tr.commit();
                }catch(Exception e){
            System.out.println(e);
        }
    }

}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/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:3305/db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>

<mapping resource="Testpojo.hbm.xml"/>
</session-factory>

</hibernate-configuration>


mapping file


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
 <hibernate-mapping>
 <class name="Testpojo" table="t">
 <id name="number" column="num">
 <generator class="native"></generator>
 </id>`enter code here`
 <property name="name" column="name"></property>
 </class>
 </hibernate-mapping>

使用Testpojo.java,Testpojo.hbm.xml,Testmain.java和hibernatecfg.xml创建一个简单的hibernate应用程序。 如何解决&#34; javax.persistence.OptimisticLockException:&#34;在休眠? 添加所有hibernate的jar文件,但仍然找到了这个error.javax.persistence.OptimisticLockException

1 个答案:

答案 0 :(得分:0)

你有OptimisticLockException意味着其他一些交易已经在使用那条特定的记录。