我在“MailAccount”类与“IncomingServer”和“OutgoingServer”类之间有两个一对一的关系。
(这是在Tomcat和Ubuntu服务器版本上运行的Java应用程序)。
映射如下所示:
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>
类层次结构如下所示:
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{
// ...
}
现在,出现了问题:
尽管我的应用程序大多数时候运行良好,但是似乎有一种情况会删除电子邮件服务器,但是相应的帐户却没有,那就是进行此调用的时间:
session.delete(mailAccountInstance);
在Hibernate的一对一关系中,邮件帐户与其服务器之间的主键必须相等,否则,该关系将完全不同步:
示例:
想象一下,表中充满了这样的数据:
表“MailAccount”(当前auto_increment值:2)
MAIL_ACCOUNT_ID NAME
0 Account1
1 Account2
表“IncomingMailServer”(当前auto_increment值:2)
MAIL_SERVER_ID MAIL_ACCOUNT_ID
0 0
1 1
现在,删除ID = 1的帐户图像并添加新帐户。然后发生以下情况:
表“MailAccount”(当前auto_increment值:3)
MAIL_ACCOUNT_ID NAME
0 Account1
1 Account2
2 Account3
表“IncomingMailServer”(当前auto_increment值:2)
MAIL_SERVER_ID MAIL_ACCOUNT_ID
0 0
1 2
这完全弄乱了我的数据库一致性。
如何避免这种情况?
最佳答案
如果要共享主键,则只能使用一次本机ID生成器。首先创建邮件帐户,该帐户将生成其自己的ID,但是当您创建Incoming-或OutgoingMailServer时,这些需要从mailAccount属性获取其ID。
因此,您需要“外部”生成器:
<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>
您不需要MAIL_ACCOUNT_ID列,因为无论如何它总是与MAIL_SERVER_ID相同。
非常基本地遵循有关bidirectional one-to-one association on a primary key的参考。