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 6630001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:20:28+00:00 2026-05-25T22:20:28+00:00

I have the following class hierachy: public class MailAccount{ IncomingMailServer incomingServer; OutgoingMailServer outgoingServer; }

  • 0

I have the following class hierachy:

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{
 // ...
}

My (simplified) mapping file looks like this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

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

        <component name="incomingServer">
            <component name="hostAddress">
                <property name="address" column="IS_HOST_ADDRESS"></property>
            </component>

            <component name="port">
                <property name="portNumber" column="IS_PORT_NUMBER"></property>
            </component>
        </component>

        <component name="outgoingServer">
            <component name="hostAddress">
                <property name="address" column="OS_HOST_ADDRESS"></property>
            </component>

            <component name="port">
                <property name="portNumber" column="OS_PORT_NUMBER"></property>
            </component>
        </component>

    </class>
</hibernate-mapping>

The problem: Hibernate throws this exception when I call session.save(mailAccountInstance);:

org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: IncomingMailServer

So, I added the following lines to the incomingServer component:

<discriminator column="SERVER_TYPE" type="string"/>
<subclass name="ImapServer" extends="IncomingMailServer" discriminator-value="IMAP_SERVER" />           
<subclass name="Pop3Server" extends="IncomingMailServer" discriminator-value="POP3_SERVER" />

And to the outgoing server:

<discriminator column="SERVER_TYPE" type="string"/>
<subclass name="SmtpServer" extends="OutgoingMailServer" discriminator-value="SMTP_SERVER" />

But now, Hibernate gives me this error message:

org.xml.sax.SAXParseException: The content of element type "component" must match "(meta*,tuplizer*,parent?,(property|many-to-one|one-to-one|component|dynamic-component|any|map|set|list|bag|array|primitive-array)*)".

Obviously, Hibernate does not like these tags in components.

How could I work around this?

Ps: I already tried moving IncomingServer and OutgoingServer each to their own tables and map them via a one-to-one. That works but leads to inconsistencies in the database because I noticed that MailAccount and IncomingServer/OutgoingServer must always have the same primary key id. If not, everything gets out of sync and the autoincrement value for the primary keys don’t match any more (between Mailaccount and Servers).

  • 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-25T22:20:28+00:00Added an answer on May 25, 2026 at 10:20 pm

    This mapping worked for me:

    Hibernate Mapping

    <class name="MailServer" abstract="true" table="SERVER">
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <discriminator column="SERVER_TYPE" type="string"/>
        <subclass name="IncomingMailServer" abstract="true">
            <property name="address" column="IS_HOST_ADDRESS"></property>
            <property name="portNumber" column="IS_PORT_NUMBER"></property>
        </subclass>           
        <subclass name="OutgoingMailServer" abstract="true">
               <property name="address" column="OS_HOST_ADDRESS"></property>
               <property name="portNumber" column="OS_PORT_NUMBER"></property>
        </subclass>           
    </class>
    
    <subclass name="IMAPServer" extends="com.mail.account.IncomingMailServer" 
              discriminator-value="IMAP_SERVER"/>
    <subclass name="POP3Server" extends="com.mail.account.IncomingMailServer" 
              discriminator-value="POP3_SERVER"/>
    <subclass name="SMTPServer" extends="com.mail.account.OutgoingMailServer" 
              discriminator-value="SMTP_SERVER" />
    

    Class Hierarchy

    Abstract classes:

    • MailServer is an abstract class
    • IncomingServer is an abstract class : this helped NOT specifing DISCRIMINATOR VALUE
    • OutgoingServer is an abstract class : this helped NOT specifing DISCRIMINATOR VALUE

    Concrete classes:

    • IMAPServer extends IncomingServer with DISCRIMINATOR VALUE : IMAP
    • POP3Server extends IncomingServer with DISCRIMINATOR VALUE : POP3
    • SMTPServer extends IncomingServer with DISCRIMINATOR VALUE : SMTP

    All the concrete servers are mapped to the same table “SERVER” however if need be they can be mapped to individual tables using join table.

    enter image description here

    Code Snippet

    I was able to save the instances using the following simple code snippet:

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction txn =session.beginTransaction();
        IMAPServer imap=new IMAPServer();
        imap.setAddress("SMTP_01");
        imap.setPortNumber("SMTP_PORT_0001");
        session.save(imap);
        SMTPServer smtp=new SMTPServer();
        smtp.setAddress("STMP_01");
        smtp.setPortNumber("STMP_PORT_0001");
        session.save(smtp);
    
        MailAccount account=new MailAccount();
        account.setIncomingServer(imap);
        account.setOutgoingServer(smtp);
    
        session.save(account);
        txn.commit();
    

    Let me know if this solves the problem. 🙂

    EDIT: added the mapping from MailAccount to MailServer.

    <class name="MailAccount" table="MAILACCOUNT" dynamic-update="true">
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <many-to-one name="incomingServer" column="incoming" unique="true"/>
        <many-to-one name="outgoingServer" column="outgoing" unique="true"/>
    </class>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class hierarchy: public abstract class NumberRangeParameter<T extends Comparable<T>> extends ComplexParameter{
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following class (as seen through reflector) public class W : IDisposable {
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
I have the following class hierarchy: public class RealPeople { } public class Users
We have the following class hierarchy: public interface IManager { object GetObject(int); } public
In out program, we have the following class hierarchy: public class PagePanelBase: UserControl public
Say I have the following hierarchy: public class MyClass { protected virtual void Method()
I have the following class hierarchy [BsonKnownTypes(typeof(MoveCommand))] public abstract class Command : ICommand {
I have something like following: public interface A { .... } public abstract class

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.