简体   繁体   中英

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.

Let's say I have an Employee table like this:

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

+-----------------+
|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?:

@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. Basically, you state that your 1 object is stored in 2 (or more) tables. It is the complement of @Embeddable, which states that your 2 (or more) object is stored in 1 table.

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

TL;DR - no, @SecondaryTable won't work, use @OneToMany...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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