现在增加一个员工照片表“tb_employee_photo”,保存员工的照片。该表也作为员工表的另外一个从表,而不映射为实体。在这种情况下,一个员工表,有两个从表合同表和员工照片表。
员工照片表的表结构的SQL语句如以下所示:
CREATE TABLE tb_employee_photo (
id int(20) NOT NULL auto_increment,
data blob ,
employee_id int(20),
PRIMARY KEY (id)
);
此时员工实体的代码修改如以下所示:
/**员工实体*/
@Entity
/**主表为tb_employee员工表*/
@Table(name = "tb_employee")
/**
* 从表为tb_contract合同表, tb_contract表中的employee_id关联tb_employee的主键
* 从表为tb_employee_photo合同表, tb_employee_photo表中的employee_id关联tb_employee的主键
*/
@SecondaryTables({
@SecondaryTable(
name = "tb_contract",
pkJoinColumns = { @PrimaryKeyJoinColumn(name = "employee_id") }
),
@SecondaryTable(
name = "tb_employee_photo",
pkJoinColumns = { @PrimaryKeyJoinColumn(name = "employee_id") }
)
})
public class EmployeeEO implements Serializable {
public EmployeeEO() {
}
private Integer id;
……中间代码省略
private byte[] photo;
/**员工照片*/
@Lob
@Basic(fetch=FetchType.LAZY)
@Column(name = "data", table = "tb_employee_photo")
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
}