}
package com.demo.beans;
import java.util.Collection;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@EntityListeners(MyMonitor.class)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String employeeName;
@ElementCollection(targetClass = EmployeePeriod.class)
private Collection employeePeriod;
@ElementCollection
private Set<String> nickName;
public Collection getEmployeePeriod() {
return employeePeriod;
}
public void setEmployeePeriod(Collection employeePeriod) {
this.employeePeriod = employeePeriod;
}
public Set<String> getNickName() {
return nickName;
}
public void setNickName(Set<String> nickName) {
this.nickName = nickName;
}
public String getEmployeeName() {
return employeeName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}
创建employee对象
// Create an Employee Period Instance
EmployeePeriod period = new EmployeePeriod();
period.setStartDate(new Date());
period.setEndDate(new Date());
List<EmployeePeriod> lists = new LinkedList<EmployeePeriod>();
lists.add(period);
Set<String> sets = new HashSet<String>();
sets.add("bai");
sets.add("xiaoyu");
employee.setNickName(sets);
employee.setEmployeePeriod(lists);
// employee.setEmployeePeriod(period);
userRepository.addEmployee(employee);
保存后的数据库表信息如下:
mysql> select * from employee;
+----+--------------+------------+------------+
| id | employeeName | end | begin |
+----+--------------+------------+------------+|
| 1 | John Smith | NULL | NULL |
+----+--------------+------------+------------+
mysql> select * from employee_nickname;
+-------------+----------+
| Employee_id | nickName |
+-------------+----------+
| 4 | bai |
| 4 | xiaoyu |
+-------------+----------+
mysql> select * from employee_employeeperiod;
+-------------+-----------------+-------------------+
| Employee_id | EmployeeEndDate | EmployeeStartDate |
+-------------+-----------------+-------------------+
| 3 | 2015-02-13 | 2015-02-13 |
| 4 | 2015-02-13 | 2015-02-13 |
+-------------+-----------------+-------------------+
mysql> select * from TABLE_CONSTRAINTS where table_schema='test';
+--------------------+-------------------+------------------------------+--------------+-------------------------+-----------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
+--------------------+-------------------+------------------------------+--------------+-------------------------+-----------------+
| def | test | PRIMARY | test | employee | PRIMARY KEY |
| def | test | FK_1pxsqqta9epq8xd7ht1st8yxf | test | employee_employeeperiod | FOREIGN KEY |
| def | test | FK_f3slu53dbxkjrr7uejqhoxgf4 | test | employee_nickname | FOREIGN KEY |
可以使用@CollectionTable指定集合表名字@AtriibulteOverride来自定义列的名字