简体   繁体   English

与Hibernate上的@SecondaryTable有多对一的关系

[英]Many to one relationship with @SecondaryTable on Hibernate

I'm modifying a legacy db and I have to split one table into two. 我正在修改遗留数据库,我必须将一个表分成两个。 There's a lot of code referencing to my entity class so I want to use @SecondaryTable to avoid making a mess with all the project. 有很多代码引用我的实体类,所以我想使用@SecondaryTable来避免弄乱所有项目。

Let's say I have an Employee table like this: 假设我有一个像这样的Employee表:

+---------------+
|Employee       |
|---------------+
|employee_id    |(pk)(ai)
|first_name     |
|last_name      |
|email_personal |
|email_work     |
+---------------+

But now every employee can have 3, 4 or 5 email addresses, so instead of making more columns in the Employee table, we want to take them to a separate table called Email_Address like this: 但现在每个员工都可以拥有3个,4个或5个电子邮件地址,因此我们不想在Employee表中创建更多列,而是将它们带到一个名为Email_Address的单独表中,如下所示:

+-----------------+
|Email_Address    |
|-----------------+
|email_address_id | (pk)(ai)
|employee_id      | (fk)
|email_address    |
|description      |
+-----------------+

So a register that used to be like this: 所以一个寄存器过去是这样的:

+---------------+---------------+---------------+-------------------+---------------+
|  employee_id  |  first_name   |  last_name    |   email_personal  |  email_work   |
|---------------+---------------+---------------+-------------------+---------------+
|       1       |      John     |      Locke    | john@personal.com | john@work.com |
+---------------+---------------+---------------+-------------------+---------------+

Is now like this: 现在是这样的:

+---------------+---------------+---------------+
|  employee_id  |  first_name   |   last_name   |
|---------------+---------------+---------------+
|       1       |      John     |      Locke    |
+---------------+---------------+---------------+

+--------------------+---------------+-------------------+-------------------+
|  email_address_id  |  employee_id  |   email_address   |    description    |
|--------------------+---------------+-------------------+-------------------+
|       1            |       1       | john@personal.com |      Personal     |
+--------------------+---------------+-------------------+-------------------+
|       2            |       1       |   john@work.com   |        Work       |
+--------------------+---------------+-------------------+-------------------+

Finally my question: Can I use @SecondaryTable to solve this problem if my current entity class is something like this?: 最后我的问题:如果我当前的实体类是这样的话,我可以使用@SecondaryTable来解决这个问题吗?:

@Entity
@Table(name = "Employee")
public class Employee implements java.io.Serializable {

  @Id
  @Column(name = "employee_id")
  private Integer patientId;

  @Column(name = "first_name")
  private String firstName;

  @Column(name = "last_name")
  private String lastName;

  @Column(name = "email_personal")
  private String emailPersonal;

  @Column(name = "email_work")
  private String emailWork;

  .
  .
  .
}

@SecondaryTable works only if you have a one to one relationship between the primary and the secondary. @SecondaryTable仅在主要和次要之间具有一对一关系时才有效。 Basically, you state that your 1 object is stored in 2 (or more) tables. 基本上,您声明您的1个对象存储在2个(或更多)表中。 It is the complement of @Embeddable, which states that your 2 (or more) object is stored in 1 table. 它是@Embeddable的补充,表示您的2(或更多)对象存储在1个表中。

If you are refactoring legacy code and don't want to deal with all the instances of address changing from 1 to multiple, you can mark an address as "home" or "primary", and have "getAddress()" return that home or primary, which it used to do. 如果您要重构遗留代码并且不想处理从1到多个地址的所有地址实例,您可以将地址标记为“home”或“primary”,并让“getAddress()”返回该地址或以前,它曾经做过。 You would create another getter (make sure to mark it @Transient if you use getter definition) called getAllAddresses() which will return the List or Set as defined. 您将创建另一个getter(确保将其标记为@Transient,如果您使用getter定义),名为getAllAddresses(),它将返回List或Set定义。

TL;DR - no, @SecondaryTable won't work, use @OneToMany... TL; DR - 不,@ SecondaryTable不起作用,使用@OneToMany ......

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

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