如何通过Hibernate保存正确的鉴别器值

时间:2011-05-03 12:22:24

标签: java hibernate

我目前正在尝试使用Hibernate将我的Java类层次结构映射到我的数据库中,但是我继续使用以下(子)类失败:

我有一个名为MailAccount的类,它有3个属性(参见下面的代码):

public class MailAccount{
    long id;
    IncomingMailServer  incomingServer;
    OutgoingMailServer  outgoingServer;

    public MailAccount(){
        super();
    }

    // Getter and setter omitted
}

服务器类层次结构如下所示:

MailServer.java

public abstract class MailServer {
    String password;
    String host;
    String username;
    String port;

    // Getter and setter omitted
}

IncomingMailServer.java

public abstract class IncomingMailServer extends MailServer {
}

OutgoingMailServer.java

public abstract class OutgoingMailServer extends MailServer {
}

Pop3Server.java

public class Pop3Server extends IncomingMailServer{
    public Pop3Server(){
        super();
    }
}

ImapServer.java

public class ImapServer extends IncomingMailServer{
    public ImapServer(){
        super();
    }
}

SmtpServer.java

public class SmtpServer extends OutgoingMailServer{
    public SmtpServer(){
        super();
    }
}

MailAccount.java中的incomingServer和outgoingServer属性当然只包含Pop3Server,ImapServer(对于incomingServer)或SmtpServer(对于outgoingServer)的实例。

我使用以下映射配置:

MailAccount.hbm.xml

<hibernate-mapping package="test.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">
        </one-to-one>
        <one-to-one name="outgoingServer" cascade="all">
        </one-to-one>
    </class>
</hibernate-mapping>

MailServer.hbm.xml

<hibernate-mapping>
    <class name="test.server.MailServer" table="MAILSERVER" abstract="true">

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

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

        <property name="password" column="PASSWORD" />
        <property name="host" column="HOST" />
        <property name="username" column="USERNAME" />
        <property name="port" column="PORT" />

        <one-to-one name="mailAccount" class="test.account.MailAccount" foreign-key="MAIL_SERVER_ID"></one-to-one>

        <subclass name="test.server.incoming.ImapServer" extends="test.server.incoming.IncomingMailServer" discriminator-value="IMAP_SERVER">
        </subclass>

        <subclass name="test.server.incoming.Pop3Server" extends="test.server.incoming.IncomingMailServer" discriminator-value="POP3_SERVER">
        </subclass>

        <subclass name="test.server.outgoing.SmtpServer" extends="test.server.outgoing.OutgoingMailServer" discriminator-value="SMTP_SERVER">
        </subclass>

        <subclass name="test.server.incoming.IncomingMailServer" extends="test.server.MailServer" abstract="true" discriminator-value="INCOMING_SERVER">
        </subclass>

        <subclass name="test.server.outgoing.OutgoingMailServer" extends="test.server.MailServer" abstract="true" discriminator-value="OUTGOING_SERVER">
        </subclass>

    </class>
</hibernate-mapping>

问题:每当我告诉Hibernate保存一个MailAccount实例时,就像这样:

session = getSession();
transaction = session.beginTransaction();
session.save(mailAccount);
transaction.commit();

.. Hibernate为表MailServer中的鉴别器列SERVER_TYPE正确存储了所有内容。 在此列中,Hibernate应存储“IMAP_SERVER”,“POP3_SERVER”或“SMTP_SERVER”,而是保存“INCOMING_SERVER”或“OUTGOING_SERVER”。

当我尝试从数据库加载此实体时,会发生异常(当然),因为Hibernate正在尝试实例化“IncomingMailServer”或“OutgoingMailServer”类型的对象,这些对象都是抽象的。 那么,我如何让Hibernate保存正确的类型?

示例:如果属性incomingServer拥有Pop3Server的实例,那么Hiberante应该将其存储到我的数据库中,当我加载相应的MailAccount时,我希望Hibernate重新创建Pop3Server的实例。 / p>

注意:我对Hibernate很新,这是我的第一步,所以请温柔:-)。我知道我的.hmb.xml文件可能看起来很乱,所以如果你有改进建议,请继续: - )

1 个答案:

答案 0 :(得分:2)

<subclass>元素的嵌套应该类似于继承层次结构:

<subclass name="test.server.incoming.IncomingMailServer" extends="test.server.MailServer" abstract="true" discriminator-value="INCOMING_SERVER">
    <subclass name="test.server.incoming.ImapServer" extends="test.server.incoming.IncomingMailServer" discriminator-value="IMAP_SERVER">
    </subclass> 
    <subclass name="test.server.incoming.Pop3Server" extends="test.server.incoming.IncomingMailServer" discriminator-value="POP3_SERVER">
    </subclass>
</subclass>

<subclass name="test.server.outgoing.OutgoingMailServer" extends="test.server.MailServer" abstract="true" discriminator-value="OUTGOING_SERVER">
    <subclass name="test.server.outgoing.SmtpServer" extends="test.server.outgoing.OutgoingMailServer" discriminator-value="SMTP_SERVER">
    </subclass>
</subclass>