集合映射
@Embeddable//表示为一个可嵌入的对象
public class VacationEntity {
@Temporal(TemporalType.TIMESTAMP)
private Date date;
@Column(name="days")
private int dayTaken;
}
@Entity
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
private long salary;
@ElementCollection表示在Employee实体上嵌入一个 VacationEntity对象集合
//使用一个新的嵌入表来表示嵌入的集合,嵌入表的表名vacations,嵌入表的外键引用列名为empID,引用Employee实体的主键。
@CollectionTable(name ="vacations",joinColumns=@JoinColumn(name="empID"))
private Collection<VacationEntity> vacationBookings;
}
运行此程序将创建的两个表为
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`salary` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `vacations` (
`empID` int(11) NOT NULL,
`date` datetime DEFAULT NULL,
`days` int(11) DEFAULT NULL,
KEY `FKD5003936B10E0B2A` (`empID`),
CONSTRAINT `FKD5003936B10E0B2A` FOREIGN KEY (`empID`) REFERENCES `employee2` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@Entity
public class Deparment{
@OneToMany(mappedBy="dept")
@OrderBy(" salary DESC , name ASC")//如果有一个实体Deparment为一方,有一个employee集合属性,可以先根据salary降序,name升序对集合排序。
private List< Employee >employee;
}
Map集合嵌入实体
Map泛型为<String,String>
@Entity
public class Employee3 {一个人拥有多个手机型号,每个型号号下有多个手机(多个手机号)
@Id
private int id;
private String name;
@ElementCollection
@CollectionTable(name="employee_phone")//表示嵌入集合的表名称
@MapKeyColumn(name="type")// 表示map集合中key的映射到表中列的名称
@Column(name="phoneNumber")//表示map集合中的value映射到表中列的名称
private Map<String, String> phoneNumbers;
}
生成集合表结构如下,employee3和type作为联合主键。
CREATE TABLE `employee_phone` (
`Employee3` int(11) NOT NULL,
`phoneNumber` varchar(255) DEFAULT NULL,
`type` varchar(255) NOT NULL,
PRIMARY KEY (`Employee3`,`type`),
KEY `FK5BD24C9D4885768D` (`Employee3`),
CONSTRAINT `FK5BD24C9D4885768D` FOREIGN KEY (`Employee3`) REFERENCES `employee3` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Map泛型为<String,Employee4>表示一个部门有多个员工,部门中还分俱乐部,可以准确知道员工在哪个部门的那个俱乐部。
@Entity
public class Employee4 {
@Id
private int id;
private String name;
@ManyToOne
private Department department;}
public class Department {
@Id
private int id;
private String name;
@OneToMany(mappedBy="department")
@MapKeyColumn(name="cub_id")
private Map<String, Employee4> employeesByClub;
}
生成的员工表如下
CREATE TABLE `employee4` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`department` int(11) DEFAULT NULL,
`cub_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK14AC0F26193DEF01` (`department`),
CONSTRAINT `FK14AC0F26193DEF01` FOREIGN KEY (`department`) REFERENCES `department` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Map泛型为<String,Employee4>表示一个部门有多个员工,同时一个员工也可以再 多个部门工作,同时在部门中还分俱乐部。
可以用如下两个实体建表。在关系表中,可以知道某个员工在某个部门的某个俱乐部中。
@Entity
public class Employee4 {
@Id
private int id;
private String name;
}
@Entity
public class Department {
@Id
private int id;
private String name;
@ManyToMany
@JoinTable(name="dept_emp",joinColumns=@JoinColumn(name="departid"),
inverseJoinColumns=@JoinColumn(name="empid"))
@MapKeyColumn(name="cub_id")
private Map<String, Employee4> employeesByClub;
}
CREATE TABLE `dept_emp` (
`departid` int(11) NOT NULL,
`empid` int(11) NOT NULL,
`cub_id` varchar(255) NOT NULL,
PRIMARY KEY (`departid`,`cub_id`),
KEY `FK38790BCEB10E0B2C` (`empid`),
KEY `FK38790BCE1E25D3BE` (`departid`),
CONSTRAINT `FK38790BCE1E25D3BE` FOREIGN KEY (`departid`) REFERENCES `department` (`id`),
CONSTRAINT `FK38790BCEB10E0B2C` FOREIGN KEY (`empid`) REFERENCES `employee4` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8