-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate bug? Update() leads to INSERT instead of UPDATE
PostPosted: Mon May 30, 2011 7:26 am 
Newbie

Joined: Tue May 10, 2011 9:38 am
Posts: 2
Hello everyone,

I have the following weird problem with Hibernate's session.update(...) method.

I store a hierarchy of folders to the database. Each folder has a parent object (can be null) and a Set of child folders.

My mapping file looks like this:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="test.Folder" table="FOLDERS">

        <id name="id" type="long" access="field">
            <column name="FOLDER_ID" />
            <generator class="native" />
        </id>     

        <set name="childFolders" table="FOLDERS" lazy="false" inverse="true" cascade="all">
            <key column="PARENT_FOLDER_ID" not-null="false"></key>
            <one-to-many class="test.Folder" />
        </set>

        <many-to-one name="parentFolder" column="PARENT_FOLDER_ID" />

        <property name="name" column="FOLDER_NAME" />

        <property name="rootFolder" column="IS_ROOT_FOLDER" type="boolean" not-null="true" />

        <property name="path" column="FOLDER_PATH" />

        <property name="type" column="FOLDER_TYPE" />

        <property name="fullPath" column="FULL_PATH" unique="true" not-null="true" />

    </class>
</hibernate-mapping>

Now, loading the folders from the database works perfectly. All the attributes are set and the hierarchy is correct.

BUT if I call
Code:
session.update(folder)
(in a new session) on a folder that has subfolders, a JDBC exception is thrown (duplicate entry).

Quote:
java.sql.SQLException: null, message from server: "Duplicate entry 'kuk2/kuki2' for key 2"


After the exception is thrown, the child folder also has id=0 (although it had id=4 before).

So, from my observation, the following is happening (not 100% sure though):
  • session.update(parentFolder) updates the parent correctly but makes its child folder transient (sets id=0)
  • Hibernate then tries to persist the child folder. Since id=0, it does an INSERT instead of an UPDATE
  • Since the folder already exists in database, I get a duplicate entry exception because the attribute "fullPath" is unique and not-null.

Note: If folder has no subfolders and session.update(folder) is executed, everything works alright.

Can someone explain what's going on here? I already tried to remove the cascade="all" attribute in the mapping file but that had no effect.

While searching for a solution, I found this:
http://stackoverflow.com/questions/2784 ... e-children
.. which lead me to http://opensource.atlassian.com/project ... e/HHH-3332
.. which again lead me to http://opensource.atlassian.com/project ... e/HHH-5855 which doesn't seem to be solved yet.

Is there a workaround for this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.