Reputation: 1925
I would create table, which contains objects. Objects should displayed like columns in table. (+ public, - private)
+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
My unsuccessful attempt:
@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;
Advise the correct annotation for this situation. I will appreciate.
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'|
Upvotes: 0
Views: 1069
Reputation: 5632
If there is some constraint that you want to save all object(entities) in one table in your case company(but the best practice is a normal data base design you should consider company department and workers in separate tables) you have redundancy in data and your table data seems like this...
1 comp1 1 dep1 1 worker1
1 comp1 1 dep1 2 worker2
1 comp1 2 dep2 3 worker3
then correct jpa annotation is:
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name="company")
public class Company{
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@XmlAttribute(name="idComapny")
@Column (name="idCompany")
private int idcompany;
@XmlElement(name="companyName")
@Column (name="companyName")
private String companyName;
**@Embedded**
private Department department;
**@Embedded**
private Worker worker;//can be removed and put in Department but result is the same
......
and put annotation @Embeddable on top of Department Entity and Worker Entity.
@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;
Upvotes: 4