-->
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: How to model a double parent/child relationship
PostPosted: Tue May 10, 2011 9:43 am 
Newbie

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

I got a pretty simple parent/child relationship here, which looks like this:

  • Email servers have n folders.
  • Folders can have n (sub-)folders.
  • Folders have a reference to their parent folder as well as to the email server they belong to.

My mapping files look like this:

MailServer.hbm.xml

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">
<!-- Generated 02.05.2011 12:32:52 by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="test.MailServer" table="MAILSERVER">

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

        <bag name="folders" table="FOLDERS" lazy="false" inverse="true" cascade="all-delete-orphan">
            <key column="MAIL_SERVER_ID"></key>
            <one-to-many class="test.Folder" />
        </bag>

    </class>
</hibernate-mapping>

Folder.hbm.xml

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">
<!-- Generated 04.05.2011 15:02:31 by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="test.Folder" table="FOLDERS">

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

        <many-to-one name="mailServer" column="MAIL_SERVER_ID" not-null="true" />

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

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

    </class>
</hibernate-mapping>

The problem I got, is the following.

Let's say, I have the following hierachy:

Code:
- MyMailServer
    Folder1
  - Folder2
      Subfolder
    Folder3

When I call
Code:
hibernateSession.save(mailServerInstance);
or
Code:
hibernateSession.update(mailServerInstance);
then Hibernate correctly stores everything to the database. The parent column id's are filled correctly. Same for all other references.

BUT when I load the data, Hibernate reloads the folder hierachy like this:

Code:
- MyMailServer
    Folder1
    Folder2
    Subfolder
    Folder3

I understand the reason: Subfolder has a reference to its MailServer and thus, Hibernate ads it there instead of the folder where it belongs to.

But, how do I solve this problem?

Thanks in advance!

Ps.: I'm pretty new to Hibernate, so please be gentle :-)


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.