gpt4 book ai didi

java - 正确的 XML 注释。 java 。联合航空航天局

转载 作者:行者123 更新时间:2023-12-01 04:11:11 24 4
gpt4 key购买 nike

有一个结构。我想以这种方式链接这三个实体:公司应包含 ID、公司名称和部门列表,每个部门都有一个 worker 列表、ID 和部门名称。每个 worker 都有姓名、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;
}

worker

@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注释的大神请给我一个解决方案。

最佳答案

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

解决方案

  • 注释属性(获取或设置方法)而不是字段(实例变量)。
  • @XmlAccessorType(XmlAccessType.FIELD) 注释字段和类.

了解更多信息

关于java - 正确的 XML 注释。 java 。联合航空航天局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com