我正在修改遺留數據庫,我必須將一個表分成兩部分。有很多代碼引用我的實體類,因此我想使用@SecondaryTable避免弄亂所有項目。與@SecondaryTable在休眠上的多對一關係
比方說,我有一個Employee表是這樣的:
+---------------+
|Employee |
|---------------+
|employee_id |(pk)(ai)
|first_name |
|last_name |
|email_personal |
|email_work |
+---------------+
但是現在每個員工可以有3個,4個或5個電子郵件地址,所以不是在Employee表讓更多的列,我們要採取他們一個單獨的表名爲EMAIL_ADDRESS這樣的:
+-----------------+
|Email_Address |
|-----------------+
|email_address_id | (pk)(ai)
|employee_id | (fk)
|email_address |
|description |
+-----------------+
這樣使用的寄存器是這樣的:
+---------------+---------------+---------------+-------------------+---------------+
| employee_id | first_name | last_name | email_personal | email_work |
|---------------+---------------+---------------+-------------------+---------------+
| 1 | John | Locke | [email protected] | [email protected] |
+---------------+---------------+---------------+-------------------+---------------+
現在是這樣的:
+---------------+---------------+---------------+
| employee_id | first_name | last_name |
|---------------+---------------+---------------+
| 1 | John | Locke |
+---------------+---------------+---------------+
+--------------------+---------------+-------------------+-------------------+
| email_address_id | employee_id | email_address | description |
|--------------------+---------------+-------------------+-------------------+
| 1 | 1 | [email protected] | Personal |
+--------------------+---------------+-------------------+-------------------+
| 2 | 1 | [email protected] | Work |
+--------------------+---------------+-------------------+-------------------+
最後我的問題:我能使用@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;
.
.
.
}