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

  • Home
  • SEARCH
  • 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 7020725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:21:21+00:00 2026-05-27T23:21:21+00:00

I have two one-to-one relations here between a class called MailAccount and the classes

  • 0

I have two one-to-one relations here between a class called “MailAccount” and the classes “IncomingServer” and “OutgoingServer”.

(It’s a Java application running on Tomcat and Ubuntu server edition).

The mapping looks like this:

MailAccount.hbm.xml

<hibernate-mapping package="com.mail.account">
    <class name="MailAccount" table="MAILACCOUNTS" dynamic-update="true">

        <id name="id" column="MAIL_ACCOUNT_ID">
            <generator class="native" />
        </id>

        <one-to-one name="incomingServer" cascade="all-delete-orphan">
        </one-to-one>
        <one-to-one name="outgoingServer" cascade="all-delete-orphan">
        </one-to-one>

    </class>
</hibernate-mapping>

IncomingMailServer.hbm.xml

<hibernate-mapping>
    <class name="com.IncomingMailServer" table="MAILSERVER_INCOMING" abstract="true">

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

        <discriminator column="SERVER_TYPE" type="string"/>

        <many-to-one name="mailAccount" column="MAIL_ACCOUNT_ID" not-null="true" unique="true" />

        <subclass name="com.ImapServer" extends="com.IncomingMailServer" discriminator-value="IMAP_SERVER" />           
        <subclass name="com.Pop3Server" extends="com.IncomingMailServer" discriminator-value="POP3_SERVER" />

    </class>
</hibernate-mapping>

OutgoingMailServer.hbm.xml

<hibernate-mapping>
    <class name="com.OutgoingMailServer" table="MAILSERVER_OUTGOING" abstract="true">

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

        <discriminator column="SERVER_TYPE" type="string"/>

        <many-to-one name="mailAccount" column="MAIL_ACCOUNT_ID" not-null="true" unique="true" />

        <subclass name="com.SmtpServer" extends="com.OutgoingMailServer" discriminator-value="SMTP_SERVER" />

    </class>
</hibernate-mapping>

The class hierarchy looks like this:

public class MailAccount{
 IncomingMailServer incomingServer;
 OutgoingMailServer outgoingServer;
}

public class MailServer{
 HostAddress hostAddress;
 Port port;
}

public class IncomingMailServer extends MailServer{
 // ...
}

public class OutgoingMailServer extends MailServer{
 // ...
}

public class ImapServer extends IncomingMailServer{
 // ...
}

public class Pop3Server extends IncomingMailServer{
 // ...
}

public class SmtpServer extends OutgoingMailServer{
 // ...
}

Now, here comes the problem:

Although most of the time my application runs well, there seems to be one situation in which email servers get deleted, but the corresponding account doesn’t and that’s when this call is made:

session.delete(mailAccountInstance);

In a one-to-one relation in Hibernate, the primary keys between mail account and its servers must be equal, if not, the relation completely gets out of sync:

Example:

Imagine, the tables are filled with data like this:

Table “MailAccount” (Current auto_increment value: 2)

MAIL_ACCOUNT_ID NAME
0               Account1
1               Account2

Table “IncomingMailServer” (Current auto_increment value: 2)

MAIL_SERVER_ID  MAIL_ACCOUNT_ID
0               0
1               1

Now, image the account with ID=1 gets deleted and new accounts get added. The following then SOMETIMES happens:

Table “MailAccount” (Current auto_increment value: 3)

MAIL_ACCOUNT_ID NAME
0               Account1
1               Account2
2               Account3

Table “IncomingMailServer” (Current auto_increment value: 2)

MAIL_SERVER_ID  MAIL_ACCOUNT_ID
0               0
1               2

This completely messes up my database consistency.
How can I avoid this?

  • 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-27T23:21:22+00:00Added an answer on May 27, 2026 at 11:21 pm

    If you want a shared primary key, you can use the native id generator only once. You create the mail account first, which will generate its own id, but when you create the Incoming- or OutgoingMailServer, these need to take their id from the mailAccount property.

    So you need the “foreign” generator:

    <class name="OutgoingMailServer">
        <id name="id" column="MAIL_SERVER_ID"> 
           <generator class="foreign"> 
               <param name="property">mailAccount</param> 
           </generator>
        </id>
        <one-to-one name="mailAccount" not-null="true" constrained="true"/>
    <class>
    

    You don’t need a MAIL_ACCOUNT_ID column, since it will always be identical to the MAIL_SERVER_ID anyway.

    Quite basic follow the reference about bidirectional one-to-one association on a primary key.

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

Sidebar

Related Questions

I have two models: Person and Relation. The second one stores information about relations
I have two DropDownListBoxes one is called ddlDay and the other is ddlMonth. As
I have two classes one of which inherits from the other. Im trying to
I have two classes and I want to establish a many-to-many assications, here is
Let's say we have two tables here , posts and comments, the relations is
I have two Doctrine entities that have a one-to-many relationship, like this: License class
I have two tables (subjects and pages) in one-to-many relations. I want to add
Lets say I have two tables - child and parent with many-to-one relation. What
I have two tables one has a three column composite key. The other needs
I have two processes one will query other for data.There will be huge amount

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.