gpt4 book ai didi

java - Hibernate 确实会创建新条目而不是更新(并非总是如此)

转载 作者:行者123 更新时间:2023-11-29 08:57:31 25 4
gpt4 key购买 nike

我面临的问题是,我的 DAO 尝试再次保存现有对象,而不是更新。 (它创建了一个新的)

这发生在 hibernateTemplate 的 saveOrUpdate 和合并方法中。传递的对象始终包含一个 ID(PK,自动递增,身份生成器类),因此它应该更新。

异常抛出:

2013-10-18 08:31:19,660 [WARN ] [JDBCExceptionReporter.java : 77] org.hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
2013-10-18 08:31:19,664 [ERROR] [JDBCExceptionReporter.java : 78] org.hibernate.util.JDBCExceptionReporter - Duplicate entry '2012-10-17-241' for key 'YEAR_PRODUCER_ID_BRAND_ID_MODEL_ID'
2013-10-18 08:31:19,666 [ERROR] [AbstractFlushingEventListener.java : 301] org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

异常在 hibernate 类中的某处抛出,因为我无法在 Debug模式下使用 try-catch 子句捕获它们。

我的问题是,这种情况并非每次都会发生。只有四五次。我怀疑我有 session 问题,但我不知道真正出了什么问题。

我的 session FLUSH_MODE 设置为 AUTO

谢谢,如果您需要更多信息,请告诉我

更新 1:实体 hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com..model.Bonus" table="bonus" catalog="cartool">
<id name="id" type="integer">
<column name="ID" />
<generator class="identity" />
</id>
<many-to-one name="brand" class="com..model.Brand" fetch="select">
<column name="BRAND_ID" />
</many-to-one>
<many-to-one name="model" class="com..model.Model" fetch="select">
<column name="MODEL_ID" />
</many-to-one>
<many-to-one name="producer" class="com..model.Producer" fetch="select">
<column name="PRODUCER_ID" />
</many-to-one>
<property name="year" type="integer">
<column name="YEAR" />
</property>
<property name="low" type="integer">
<column name="LOW" not-null="true" />
</property>
<property name="high" type="integer">
<column name="HIGH" not-null="true" />
</property>
<property name="bonusValue" type="double">
<column name="BONUS_VALUE" />
</property>
<property name="otherTextProducer" type="string">
<column name="OTHER_TEXT_PRODUCER" length="50" />
</property>
<property name="otherTextBrand" type="string">
<column name="OTHER_TEXT_BRAND" length="50" />
</property>
<property name="deleted" type="integer">
<column name="DELETED" />
</property>
</class>
</hibernate-mapping>

更新 2:Hibernate 日志记录

2013-10-18 11:18:20,637 [DEBUG] [AbstractBatcher.java : 424] org.hibernate.SQL - update cartool.bonus set BRAND_ID=?, MODEL_ID=?, PRODUCER_ID=?, YEAR=?, LOW=?, HIGH=?, BONUS_VALUE=?, OTHER_TEXT_PRODUCER=?, OTHER_TEXT_BRAND=?, DELETED=? where ID=?
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.IntegerType - binding '17' to parameter: 1
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.IntegerType - binding '252' to parameter: 2
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.IntegerType - binding '10' to parameter: 3
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.IntegerType - binding '2012' to parameter: 4
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.IntegerType - binding '400' to parameter: 5
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.IntegerType - binding '549' to parameter: 6
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.DoubleType - binding '100.0' to parameter: 7
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.StringType - binding '' to parameter: 8
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.StringType - binding '' to parameter: 9
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.IntegerType - binding '0' to parameter: 10
2013-10-18 11:18:20,637 [DEBUG] [NullableType.java : 133] org.hibernate.type.IntegerType - binding '896' to parameter: 11

这个输出紧接在异常之前

最佳答案

检查对象中的 ID 值,只有当您的对象中有 ID 值时,hib 才会更新,否则插入。

关于java - Hibernate 确实会创建新条目而不是更新(并非总是如此),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19443510/

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