I am facing the problem, that my DAO tries to save an existing object again, instead of update. (it creates a new one)
This happens with the hibernateTemplate saveOrUpdate- and merge-method. The passed object always contains an ID (PK, auto incremented, identity generator class), so it should update.
Exception thrown:
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
The exception is thrown somewhere within the hibernate classes, because I cannot catch them with a try-catch clause in debug mode.
My problem is, that this happens not every time. Only all four-five times. I suspect that I have a session problem but I have no idea what really goes wrong.
My Session FLUSH_MODE is set to AUTO
Thanks and please let me know if you need some further informations
Update 1: The entity 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>
Update 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
This output comes immediately before the exception