这种情况的正确注释是什么。 JPA。过冬

时间:2013-11-10 19:29:51

标签: java hibernate jpa

我会创建包含对象的表。对象应该像表格中的列一样显示。 (+公开, - 私人)

   +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'|

1 个答案:

答案 0 :(得分:4)

如果您希望将所有对象(实体)保存在案例公司的一个表中(但最佳做法是通常的数据库设计,您应该考虑公司部门和工作人员在单独的表中),那么您有冗余在数据和你的表格数据似乎这样......

1 comp1 1 dep1 1 worker1

1 comp1 1 dep1 2 worker2

1 comp1 2 dep2 3 worker3

然后更正jpa注释:

@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
......

并在部门实体和工作人员实体之上添加注释 @Embeddable

@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;