gpt4 book ai didi

java - jpa "dirty"交易

转载 作者:行者123 更新时间:2023-12-01 05:10:33 25 4
gpt4 key购买 nike

我使用 java、jpa(和 hibernate)编写了一个桌面应用程序。

我有一个页面,用户可以在其中编辑 bean 字段并将其保存到数据库。 bean 的字段codice 是唯一的。数据库有 2 行,第一行包含 codice = 0,第二行包含 codice = 00

用途是编辑第一个bean 0 并将codice 字段更改为00,并尝试持久化它。 entityManager.commit() 会引发异常:

错误: key “codice”的条目“00”重复

异常被捕获并显示给用户,它将 codice 字段修改为 11 (尚不存在)并尝试保留。

entityManager.commit() 再次引发相同的异常

(错误: key 重复输入“00”)

就像它再次尝试保留旧项目一样。

这里是一个案例:

EntityManager em = Persistence.createEntityManagerFactory(
"testNewDataModel").createEntityManager();
Articolo a = em.find(Articolo.class, 1L);
EntityTransaction tx = em.getTransaction();
a.setNome("albi8");
// ---- 1 ---- just a normal commit, no errors
tx.begin();
a = em.merge(a);
tx.commit();
System.out.println("---- 1 ----");
// ---- 2 ---- will cause Duplicate entry error
tx.begin();
a.setCodice("00"); // already exist an item with codice = 00 -> Duplicate entry '00' for key 'codice'
a = em.merge(a);
try {
System.out.println("---- 2 ---- pre commit");
tx.commit();
System.out.println("---- 2 ---- after commit");
} catch (Exception e) {
System.out.println("Duplicate entry '00' for key 'codice'");
}

// ---- 3 ---- trying to correct the previous error

tx.begin();

a.setCodice("11"); //this code don't exist yet
a.setNome("alby9");

a = em.merge(a);

try {
System.out.println("---- 3 ---- pre commit");
tx.commit();
System.out.println("---- 3 ---- after commit");
} catch (Exception e) {
e.printStackTrace(); //Duplicate entry '00' for key 'codice'
}

她的输出:

org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
22-ago-2012 18.41.02 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select articolo0_._id as column1_12_0_, articolo0_.id_categoria as id10_12_0_, articolo0_.codice as codice12_0_, articolo0_.coefficiente_prezzo as coeffici3_12_0_, articolo0_.descrizione as descrizi4_12_0_, articolo0_.id_foto as id5_12_0_, articolo0_.nome as nome12_0_, articolo0_.posizione as posizione12_0_, articolo0_.prezzo_imponibile as prezzo8_12_0_, articolo0_.prezzo_vendita as prezzo9_12_0_, articolo0_1_.quantita as quantita13_0_ from inventario4.articolo articolo0_ left outer join inventario4.magazzino articolo0_1_ on articolo0_._id=articolo0_1_._id_articolo where articolo0_._id=?
---- 1 ----
---- 2 ---- pre commit
Hibernate: update inventario4.articolo set id_categoria=?, codice=?, coefficiente_prezzo=?, descrizione=?, id_foto=?, nome=?, posizione=?, prezzo_imponibile=?, prezzo_vendita=? where _id=?
22-ago-2012 18.41.02 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1062, SQLState: 23000
22-ago-2012 18.41.02 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Duplicate entry '00' for key 'codice'
Duplicate entry '00' for key 'codice'
---- 3 ---- pre commit
Hibernate: update inventario4.articolo set id_categoria=?, codice=?, coefficiente_prezzo=?, descrizione=?, id_foto=?, nome=?, posizione=?, prezzo_imponibile=?, prezzo_vendita=? where _id=?
22-ago-2012 18.41.02 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1062, SQLState: 23000
22-ago-2012 18.41.02 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Duplicate entry '00' for key 'codice'
javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:90)
at org.lelmarir.test.Main.main(Main.java:79)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry '00' for key 'codice'
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:78)
... 1 more
Caused by: org.hibernate.exception.ConstraintViolationException: Duplicate entry '00' for key 'codice'
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy21.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3006)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2908)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3237)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:113)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:264)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:187)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1081)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:315)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73)
... 1 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '00' for key 'codice'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 18 more

最后是问题:为什么事务仍然尝试使用 codice = 00 持久化一行?

最佳答案

我怀疑问题在于您在事务之间重用了相同的对象。我要么清除实体管理器,要么使用find加载新的对象。但我不确定,我已经很长时间没有使用JPA了。

关于java - jpa "dirty"交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12077700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com