我正在开展一个新项目,并且遇到以下情况:我需要插入一个新员工的新公司。在一种形式中,用户能够注册新的小公司。 使用:Spring Boot 2.0与Spring Data REST和Hibernate(BackEnd),React(FrontEnd)
我使用以下结构:
实体:
@Entity
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Column(name = "name")
private String name;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "id_company_address", nullable = false)
private CompanyAddress companyAddress;
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy="company")
private List<Employee> employeeList = new ArrayList<Employee>();
/*
* Getters and Setters
*/
}
@Entity
public class CompanyAddress {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_company_address")
private Long id;
@NotBlank
@Column(name = "street")
private String street;
@ManyToOne(fetch = FetchType.EAGER, cascade= { CascadeType.MERGE })
@JoinColumn(name="id_country", nullable = false)
private Country country;
/*
* Getters and Setters
*/
}
@Entity
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_country")
private Integer id;
@NotBlank
@Column(name = "name", length = 255)
private String name;
/*
* Getters and Setters
*/
}
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Column(name = "name")
private String name;
@ManyToOne
@JoinColumn(name="id_company", nullable = false)
private Company company;
/*
* Getters and Setters
*/
}
存储库:
@RepositoryRestResource(collectionResourceRel = "company", path = "companies", excerptProjection = CompanyProjection.class)
public interface CompanyRepository extends PagingAndSortingRepository<Company, Long> {}
@RepositoryRestResource(collectionResourceRel = "employee", path = "employees", excerptProjection = EmployeeProjection.class)
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {}
JSON(man manuualy for test):
const company = {
name: 'test',
companyAddress: {
country: http://localhost:8080/api/countries/1,
street: 'Street test'
}
employeeList: [{
name: 'test 01'
},
{
name: 'test 02'
}]
}
我遇到以下错误&#34; JSON解析错误:无法从类型[java.net.URI]转换为类型[com.project.model.Employee]以获取值&#39; name& #39 ;;&#34;
我知道我应该在已经保留数据时发送URI,以下是一个示例:
const company = {
name: 'test',
employeeList: [http://localhost:8080/api/countries/1]
}
我的问题是员工还没有保存,我需要用新员工保存新公司,所以我没有URI。
我尝试使用注释&#39; exported = false&#39;在Employee存储库中(正如我在某些论坛中看到的那样),例如:
@RepositoryRestResource(collectionResourceRel = "employee", path = "employees", excerptProjection = EmployeeProjection.class, exported = false)
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {}
它有效,但我不再需要在Employee存储库中保存/查找。所以,它对我的情况不起作用。
是否有正确的方法可以同时保存新公司的新员工名单?
答案 0 :(得分:0)
您的映射关系很好。你可以尝试这种方法。
@RequestMapping(method= RequestMethod.POST, path="/create")
public String createCompany(@RequestBody Company companyJson) {
String companyName = companyJson.getName();
Company alreadyExistsCompany = companyRepository.findByName(companyName);
Company company = null;
if(alreadyExistsCompany == null){
Company newCompany = new Company ();
newCompany.setName(companyName);
company = companyRepository.save(newCompany);
} else{
company = alreadyExistsCompany;
}
for (Employee employee : companyJson.getEmployeeList()){
employee.setCompany (company);
employeeRepository.save (employee);
}
return "done";
}
你的json请求应该是这样的:
{
"name": "test company 02",
"employeeList": [
{ "name": "employee 01"},
{ "name": "employee 02"}
]
}