0

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

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

1 回答 1

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放在 Department Entity 和 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;
于 2013-11-10T20:01:27.930 回答