开发者

Hibernate's session.update(obj) method makes child objects transient (in parent/child relationship)

I have a parent-/child relationship of folders, which looks like this:

A folder can have 0-1 parent folders. A folder can have 0-n child folders (subfolders).

Using Hibernate, I call session.update(folder) on these folders.

When such a folder has NO subfolders, everything works alright.

BUT when a folder has subfolders, session.update(folder) will make the subfolder(s) transient (subfolder's id changes from 4 to 0!).

How can that be?

Here is my mapping file:

<?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="none">
            <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" />

        &l开发者_如何学Ct;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>

Update: Here is the Java code I use to update the folder:

public class DatabaseController{

    private SessionFactory  sessionFactory  = null;

    public void updateFolder(Folder folder){
        Session session = null;
        Transaction transaction = null;
        try {
            session = getSession();
            transaction = session.beginTransaction();
            session.update(folder);
            transaction.commit();
        } catch (Exception e) {
            rollback(transaction);
            closeSession();
        } finally {
            closeSession();
        }
    }

    /*
     * Returns the Hibernate session
     */
    private Session getSession() {
        if (_session == null) {
            _session = getSessionFactory().getCurrentSession();
        }
        if (_session.isOpen() == false) {
            _session = getSessionFactory().openSession();
        }
        return _session;
    }

    /**
     * Returns the session factory
     * 
     * @return The session factory
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}


That's correct

you have cascade="none" in your set definitions.

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#objectstate-transitive

You need to set cascade="save-update".


I found a solution to the problem. Changing the following lines in my hibernate mapping file fixed the issue:

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

I think, removing not-null="false" for the key did the trick here.


your code : UPDATE 2

 <set name="childFolders" table="FOLDERS" lazy="false" inverse="true" cascade="none">
            <key column="PARENT_FOLDER_ID" not-null="false"></key>
            <one-to-many class="test.Folder" /> // **this should point to child table not itself**
  </set>

you have given wrong relation ship my code :

<set cascade="all, delete-orphan" name="childTable" order-by="param" inverse="true">
  <key>
    <column name="p_id"/>
  </key>
  <one-to-many class="com.a.data.ChildTable"/>
</set>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜