1

構造があります。私はこのように 3 つのエンティティをリンクしたいと考えています。会社には ID、会社名、部門のリストが含まれている必要があり、各部門には従業員のリスト、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 に答える