2

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

Tunguska
  • 1,205
  • 3
  • 18
  • 37
  • `YEAR_PRODUCER_ID_BRAND_ID_MODEL_ID` does sound like compound key. Can we see the entity? – Pavel Horal Oct 18 '13 at 07:14
  • Yes you are right, it is compound unique key. But I think this is not the problem. The key fires because hibernate tries to insert an already existing entry. If hibernate would update instead of create a new entry the key would never fire. – Tunguska Oct 18 '13 at 08:28
  • 1
    I found some similar problems: http://www.programmingforfuture.com/2013/05/warning-sql-error-1062-sqlstate-23000.html http://stackoverflow.com/questions/1105734/hibernate-save-strange-behaviour Are you sure, that the physical schema is like you wanted to define it? – Andreas Aumayr Oct 18 '13 at 08:59
  • Thats weird. I enabled the hibernate logging and as you can see, hibernate does an update. And all parameters are correct, but unique constraint is nevertheless fired. – Tunguska Oct 18 '13 at 09:29
  • Can we see the unique index definition? – Pavel Horal Oct 18 '13 at 09:52
  • Should I have a index definition in hibernate? I have only one in the mySQL table: UNIQUE INDEX `YEAR_PRODUCER_ID_BRAND_ID_MODEL_ID` (`YEAR`, `PRODUCER_ID`, `BRAND_ID`, `MODEL_ID`), – Tunguska Oct 18 '13 at 10:15
  • m not used to the xml mappings, but then have you defined that the id is a primary key ? "@Id" "@GeneratedValue" this sort of a thing ??, yes it is, i just saw it – Rat-a-tat-a-tat Ratatouille Oct 18 '13 at 10:45
  • I was wrong. As I can see now, the unique index as it is, is wrong in this context. Thanks for your help guys! – Tunguska Oct 18 '13 at 11:04
  • How can I upvote your comments? =) – Tunguska Oct 18 '13 at 11:18
  • up the comments lol :D – Rat-a-tat-a-tat Ratatouille Oct 18 '13 at 11:35
  • There are no arrows visible... ;-) – Tunguska Oct 18 '13 at 11:44

1 Answers1

0

Check ID value in object, hib will update only if you have ID value in your object otherwise insert.

  • The ID of the object is always set. When the update proceeds, as well if not. The ID is bounded properly in JSP and the ID generator class in the hbm is set to "identity". So i think there is nothing wrong with the ID. – Tunguska Oct 18 '13 at 08:18