2017-05-27 494 views
0

错误:如何在hibernate中解决“javax.persistence.OptimisticLockException:”?

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和Hibernate的一个简单appliaction hibernatecfg.xml。 如何解决“javax.persistence.OptimisticLockException:”在休眠? 添加所有的休眠jar文件,但仍然发现这error.javax.persistence.OptimisticLockException

+1

欢迎!请修改你的问题。目前还不清楚到底你的问题到底是什么以及你做了什么来解决问题。你能将代码减少到基本点吗? –

回答

0

你得到OptimisticLockException意味着一些其他transcation已经在使用该特定的记录。

+0

所以现在我做什么来解决它? @Anand –

+0

你的例子是不够的,请提供你正在做CURD操作的方法的更多细节,因为当一个特定的记录发生多个操作时会发生OptimisticLockException。 –

+0

其解决谢谢 –

相关问题