Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5981741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:59:11+00:00 2026-05-22T21:59:11+00:00

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

  • 0

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

<?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

<?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:

- MyMailServer
    Folder1
  - Folder2
      Subfolder
    Folder3

When I call hibernateSession.save(mailServerInstance); or hibernateSession.update(mailServerInstance);, 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:

- 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:59:12+00:00Added an answer on May 22, 2026 at 9:59 pm

    I worked around this by replacing

    <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>
    

    in MailServer.hbm.xml with

    <one-to-one name="rootFolder" class="test.Folder" cascade="all" />
    

    That did the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a pretty simple site (around 8 pages) with only one way page
I have a very simple function which takes in a matching bitfield, a grid,
I have a pretty simple local service that I'm trying to bind to my
The 'Like' is pretty simple it seems, it only supports ? and * I
I think I have a pretty simple goal but cant seem to reach it.
I've got a pretty basic question about how to start using Castle Windsor. I've
Pretty self-explanatory, I tried google and got a lot of the dreaded expertsexchange, I
Today I got my book Head First Design Patterns in the mail. Pretty interesting
Got myself in a bit of a pickle here ... working on a CMS
Got a bit of a mind freeze at the moment. I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.