我有以下课程
//公司
@Entity
@Table(name=Company.TABLE_NAME)
public class Company {
public static final String TABLE_NAME = "company";
public static final String PK_NAME = "id";
@Id
@Column(name="id")
private String id = IdGenerator.createId();
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
@JoinColumn(name="categoryid", table=TABLE_NAME)
private Category category;
@Column(name = "name", nullable=false, table=TABLE_NAME)
@Size(min=2, max=60, message="LATER")
private String name;
@Column(name="domainName", nullable=true, table=TABLE_NAME)
private String domainName;
@Column(name="registrationNumber", nullable=true, table=TABLE_NAME)
private String registrationNumber;
@Column(name="description", table=TABLE_NAME)
private String description;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="itemId")
private Set<Image> logos = new HashSet<>();
@OneToMany(mappedBy="company", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<Branch> branches = new ArrayList<>();
public Company() {}
public void addBranch(Branch branch) {
branch.setCompany(this);
branch.getAddress().setActor(this);
branches.add(branch);
}
public void addLogo(Image logo) {
}
// GETTERS AND SETTERS
}
//分支机构
@Entity
@Table(name=Branch.TABLE_NAME)
public class Branch {
public static final String TABLE_NAME = "branch";
@Id
@Column(name = "id")
private String id = IdGenerator.createId();
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="address")
private Address address;
@Column(name = "telephone")
private String telephone;
@ManyToOne
@JoinColumn(name="company")
private Company company;
public Branch() {
this.address.setAddressCategory(AddressCategory.BRANCH);
}
// GETTERS AND SETTERS
}
//地址
@Entity
@Table(name=Address.TABLE_ADDRESS)
public class Address {
public static final String TABLE_ADDRESS = "address";
@Id
@Column(name="id")
private String id = IdGenerator.createId();
@Column(name="category", nullable=false)
private AddressCategory addressCategory;;
@Column(name="streetname", nullable=true)
private String streetname;
@Column(name="number", nullable=true)
private String number;
@Column(name="postcode", nullable=true)
private String postcode;
@Column(name="city", nullable=true)
private String city;
@Column(name="country", nullable=true)
private String country;
@Column(name = "telephone")
private String telephone;
@Embedded
private EmailAddress email;
@Column(name="description", nullable=true)
private String description;
// GETTERS AND SETTERS
}
//电子邮件
@Embeddable
public class EmailAddress {
@Column(name="email")
private String emailAddress;
public EmailAddress() { }
public EmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getEmailAddress() { return emailAddress; }
public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; }
}
//公司DTO
public class CompanyDTO {
private String id;
private String categoryId;
private String name;
private String domainName;
private String registrationNumber;
private String description;
private List<Branch> branches = new ArrayList<>();
public CompanyDTO fromCompany(Company company) {
this.setId(company.getId());
this.setName(company.getName());
this.setDomainName(company.getDomainName());
this.setRegistrationNumber(company.getRegistrationNumber());
this.setDescription(company.getDescription());
return this;
}
public Company toCompany(Company company) {
company.setName(getName());
company.setDomainName(getDomainName());
company.setRegistrationNumber(getRegistrationNumber());
company.setDescription(getDescription());
//company.setLogo(new Image());
company.setBranches(this.getBranches());
return company;
}
// GETTERS AND SETTERS
}
控制器方法:
@PostMapping(path = CREATE_COMPANY)
public ResponseEntity<?> save(@RequestBody CompanyDTO dto){ System.out.println("__________COMPANY----------");
Category category = categoryService.findById(dto.getCategoryId());
Company company = dto.toCompany(new Company());
company.setCategory(category);
company.setLogos(new HashSet<Image>(Arrays.asList(new Image("garden", "garden.jpg", "/garden"))));
companyService.save(company);
return new ResponseEntity<> ("", HttpStatus.CREATED);
}
这是我从邮递员发送的JSON:
{
"categoryId": "ee6e75c9-2d1b-41c1-8f12-236fbf907683",
"name": "McDonalds",
"domainName": "Vodafone.com",
"registrationNumber": "5555555",
"description": "Vodafone",
"branches": [
{
"telephone": "003325647895",
"address": {
"addressCategory": "BRANCH",
"streetname": "Milkweg",
"number": "50",
"postcode": "3014",
"city": "Vienna",
"country": "Austria",
"email": {"email": "example@mail.com"},
"telephone": "003325647895",
"description": "P. O. Box decription",
"actor": "1abaaa11-1145-432f-84b0-40b9b551acdd"
}
}
],
"logos": []
}
我在邮递员中收到以下错误: 400错误的请求。它甚至永远不会发送到服务器,因为它是一个错误的请求。
如果我删除了数组branchs属性(空数组)中的对象,那么它将起作用。
我仔细检查了我的属性名称,但一切似乎都是正确的。我只是不知道怎么了。