我会创建包含对象的表。对象应该像表格中的列一样显示。(+ 公共,- 私人)
+Company
-int companyId
-String companyName
-Set<Department> listOfDepartments = new HashSet<Department>();
+Department
-int departmentId
-String departmentName
-Set<Worker> listOfWorkers = new HashSet<Worker>();
+Worker
-int workerId
-String workerName
我的失败尝试:
@XmlRootElement(name="Company")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name="Company")
public class Company {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@XmlAttribute (name="id")
@Column (name="idCompany")
private int idCompany;
@XmlElement(name="companyName")
@Column (name="companyName")
private String companyName;
@XmlElement (name = "YYY")
@ElementCollection
private Set<Department> listOfDepartments = new HashSet<Department>();
@XmlRootElement(name="Department")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Embeddable
@Table(name="Department")
public class Department {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@XmlAttribute(name="idDepartment")
@Column (name="idDepartment")
private int idDepartment;
@XmlElement(name="departmentName")
@Column (name="deparmentName")
private String departmentName;
@XmlElement (name = "XXX")
@ElementCollection
private Set<Worker> listOfWorkers = new HashSet<Worker>();
@XmlRootElement(name="Worker")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Embeddable
@Table(name="Worker")
public class Worker {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@XmlAttribute(name="idWorker")
@Column (name = "idWorker")
private int idWorker;
@XmlElement(name="workerName")
@Column (name = "workerName")
private String workerName;
建议针对这种情况使用正确的注释。我会感激的。
UPDATE:
companyId|companyName|deptId|deptName|workerId|workerNam|
1|'Lala'|1|'Logical'|1|'Jason'|
1|'Lala'|1|'Logical'|2|'Bason'|
1|'Lala'|2|'Chemical'|1|'Cason'|
1|'Lala'|2|'Chemical'|2|'Dason'|