1

有一个结构。我想以这种方式链接三个实体:公司应该包含 id、公司名称和部门列表,每个部门都有一个工人列表、id 和部门名称。每个工人都有姓名,身份证。

+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
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
public class Company {

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private int companyId;
    private String companyName;

    @XmlElementWrapper(name="listOfDepartments")
    @XmlElement
    @OneToMany(mappedBy = "company", cascade=CascadeType.PERSIST, fetch = FetchType.EAGER)
    private Set<Department> listOfDepartments = new HashSet<Department>();

    public Set<Department> getListOfDepartments() {
        return listOfDepartments;
    }

    public void setListOfDepartments(Set<Department> listOfDepartments) {
        this.listOfDepartments = listOfDepartments;
    }

    public Company(){}

    public Company(String companyName){
        this.companyName = companyName;
    }

    @XmlAttribute(name="id")
    public int getCompanyId() {
        return companyId;
    }

    public void setCompanyId(int companyId) {
        this.companyId = companyId;
    }

    @XmlElement(name="companyName")
    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

部门

@Entity
public class Department {

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private int idDepartment;
    private String departmentName;

    @XmlAttribute(name="companyId")
    @ManyToOne()
    @JoinColumn(name="companyId")
    private Company company;

    @XmlElementWrapper(name="listOfWorkers")
    @XmlElement
    @OneToMany(mappedBy = "department", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    private Set<Worker> listOfWorkers = new HashSet<Worker>();

    public Set<Worker> getListOfWorkers() {
        return listOfWorkers;
    }

    public void setListOfWorkers(Set<Worker> listOfWorkers) {
        this.listOfWorkers = listOfWorkers;
    }

    public Department(){}
    public Department(String departmentName, Company company){
        this.departmentName = departmentName;
        this.company = company;
    }

    @XmlAttribute(name="id")
    public int getIdDepartment() {
        return idDepartment;
    }

    public void setIdDepartment(int idDepartment) {
        this.idDepartment = idDepartment;
    }

    @XmlElement(name="departmentName")
    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

工人

@Entity
public class Worker {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private int idWorker;
    private String workerName;


    @ManyToOne
    @JoinColumn(name="departmentId")
    private Department department;



    public Worker(){}
    public Worker(String workerName,Department department){
        this.workerName = workerName;
        this.department = department;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    @XmlAttribute(name="id")
    public int getIdWorker() {
        return idWorker;
    }

    public void setIdWorker(int idWorker) {
        this.idWorker = idWorker;
    }

    @XmlElement(name="name")
    public String getWorkerName() {
        return workerName;
    }

    public void setWorkerName(String workerName) {
        this.workerName = workerName;
    }
}

我发现了错误:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions
Class has two properties of the same name "companyId"
    this problem is related to the following location:
        at public int ru.eldarkaa.dto.Company.getCompanyId()
        at ru.eldarkaa.dto.Company
    this problem is related to the following location:
        at private int ru.eldarkaa.dto.Company.companyId
        at ru.eldarkaa.dto.Company
Class has two properties of the same name "companyName"
    this problem is related to the following location:
        at public java.lang.String ru.eldarkaa.dto.Company.getCompanyName()
        at ru.eldarkaa.dto.Company
    this problem is related to the following location:
        at private java.lang.String ru.eldarkaa.dto.Company.companyName
        at ru.eldarkaa.dto.Company
Class has two properties of the same name "listOfWorkers"
    this problem is related to the following location:
        at public java.util.Set ru.eldarkaa.dto.Department.getListOfWorkers()
        at ru.eldarkaa.dto.Department
        at private java.util.Set ru.eldarkaa.dto.Company.listOfDepartments
        at ru.eldarkaa.dto.Company
    this problem is related to the following location:
        at private java.util.Set ru.eldarkaa.dto.Department.listOfWorkers
        at ru.eldarkaa.dto.Department
        at private java.util.Set ru.eldarkaa.dto.Company.listOfDepartments
        at ru.eldarkaa.dto.Company

XML注释的神请告诉我一个解决方案。

4

1 回答 1

0

默认情况下,JAXB 将公共字段和属性视为已映射。当您注释非公共字段时,它会导致该字段也被映射,从而导致冲突。

解决方案

  • 注释属性(get 或 set 方法)而不是字段(实例变量)。
  • 用 注释字段和类@XmlAccessorType(XmlAccessType.FIELD)

了解更多信息

于 2013-11-11T11:28:25.737 回答