我遇到了问题,我的DAO尝试再次保存现有对象,而不是更新。 (它会创建一个新的)
使用hibernateTemplate saveOrUpdate-和merge-method会发生这种情况。传递的对象始终包含一个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类中的某处抛出,因为我无法在调试模式下使用try-catch子句捕获它们。
我的问题是,每次都不会发生这种情况。只有全部四到五次。我怀疑我遇到会话问题,但我不知道到底出了什么问题。
我的会话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 Logging
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
此输出紧接在异常
之前答案 0 :(得分:0)
检查对象中的ID值,只有在对象中有ID值时才会更新hib,否则插入。