简体   繁体   English

为什么在带有“设置”字段的情况下使用Hibernate的示例未在数据库表中声明外键?

[英]why does this example for using Hibernate with `Set` fields not declare foreign key in the database table?

This link gives an example of how to use Hibernate with classes with Set fields. 该链接提供了一个示例,说明如何将Hibernate与带有Set字段的类一起使用。

The following is the part for defining the corresponding database tables in MySQL: 以下是在MySQL中定义相应数据库表的部分:

Define RDBMS Tables Consider a situation where we need to store our employee records in EMPLOYEE table, which would have the following structure − 定义RDBMS表考虑一种情况,我们需要将员工记录存储在EMPLOYEE表中,该表具有以下结构-

 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) ); 

Further, assume each employee can have one or more certificate associated with him/her. 此外,假设每个员工可以拥有一个或多个与其关联的证书。 So, we will store certificate related information in a separate table having the following structure − 因此,我们将与证书相关的信息存储在具有以下结构的单独表中-

 create table CERTIFICATE ( id INT NOT NULL auto_increment, certificate_name VARCHAR(30) default NULL, employee_id INT default NULL, PRIMARY KEY (id) ); 

There will be one-to-many relationship between EMPLOYEE and CERTIFICATE objects EMPLOYEE和CERTIFICATE对象之间将存在一对多关系

I was wondering why it does not declare employee_id of CERTIFICATE as a foreign key to the primary key id of table EMPLOYEE in MySQL? 我想知道为什么它不将CERTIFICATE employee_id声明为MySQL中表EMPLOYEE的主键id的外键?

Thanks. 谢谢。

Normally, applications that use an Object-Relationship Mapping (ORM) framework, such as hibernate, handle all the relationship logic themselves (ie cardinality, constraints, ect..). 通常,使用对象关系映射(ORM)框架的应用程序(例如休眠)自己处理所有关系逻辑(即基数,约束等)。 So it is possible that, as far as the database concerns, employee_id is just a single number that has no relationship with any other table, and, on the other hand, Hibernate considers it as a foreign key to the Employee table. 因此,就数据库而言,employee_id可能只是一个与其他任何表都没有关系的数字,另一方面,Hibernate将其视为Employee表的外键。 In this case, Hibernate would be the one in charge of managing all the relationship logic. 在这种情况下,Hibernate将是负责管理所有关系逻辑的人。

The advantage of this approach is that you can create certificates that are not associated to any employees at the database level. 这种方法的优势在于,您可以在数据库级别创建不与任何员工相关联的证书。 Let's say that you are migrating data to a database, and you migrate first all the certificate's table. 假设您正在将数据迁移到数据库,然后首先迁移所有证书的表。 If you had enforced the foreign key constraint, you couldn't do that: you would have to first migrate the employees' table and THEN the certificates one. 如果您已强制执行外键约束,则无法这样做:您必须首先迁移雇员表,然后再迁移证书表。 However, by following the tutorial on the link you posted, you wouldn't need to worry about this, because you can have certificates belonging "fake" employees. 但是,按照您发布的链接上的教程进行操作,您无需担心,因为您可以拥有属于“假”员工的证书。 This would happen in an intermediate transient state when you're not done migrating all your data. 当您还没有完成所有数据的迁移时,这会在中间瞬态状态下发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM