Announcement Announcement Module
Collapse
No announcement yet.
Hibernate insert problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate insert problem

    Hi,

    I'm using Spring 2.5.5, Hibernate 3.2.5 and MySQL 5 and I want to store a new row in the DB only if a row with the same unique index is not already stored, otherwise I would like to do an update on that row.

    In order to do that I'm using the "insert-on-duplicate" feature of MySQL (http://dev.mysql.com/doc/refman/5.1/...duplicate.html)
    Basically, I modified my Hibernate Mapping by adding the following

    Code:
            <sql-insert>
            insert into vtable (datetime, type, id_group, id_location, value, id_value)
            values (?, ?, ?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE id_value=LAST_INSERT_ID(id_value), value=values(value)
            </sql-insert>

    Now I have the following exception:

    Code:
    2008-12-23 14:36:02,223 ERROR [org.hibernate.jdbc.AbstractBatcher] - <Exception executing batch: >
    org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1
    Does anybody has an idea how to solve the problem?
    THANKS!

    Bye
    Last edited by andrew007; Dec 23rd, 2008, 03:49 PM.

  • #2
    Use hibernate SaveorUpdate

    use hibernate session.saveOrUpdate(Object); it will add the new record if not exist otherwise updates it.you should not use database specific techniques when using hibernate otherwise it will specific for mysql.

    Comment


    • #3
      Thank you for your answer, but saveOrUpdate insert data if the PRIMARY KEY not exist otherwise update data. That is: saveOrUpdate is based on the primary key.
      In my case I want to update if the UNIQUE INDEX (datetime, type, id_group, id_location) is not already stored...

      BYE!

      Comment


      • #4
        Solved by using a stored procedure with Hibernate...

        Code:
                <sql-insert check="none" callable="true">
                    {call saveOrUpdate (?, ?, ?, ?, ?)}
                </sql-insert>

        Comment

        Working...
        X