【发布时间】:2019-11-10 14:12:23
【问题描述】:
我正在开发一个 springboot 应用程序。
我正在尝试将自定义对象列表保存在其中一列中,但出现错误:一列上不存在关系异常
员工 DTO:
public class Employee {
private Integer employeeId;
private String employeeName;
private List<Address> addresses;
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
}
地址 DTO:
public class Address {
private String state;
private String country;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Address(String state, String country) {
super();
this.state = state;
this.country = country;
}
public Address() {
}
}
员工服务:
@Service
public class EmployeeService {
@Autowired
private UserRepository userRepository;
public void persist() {
Employee employee = new Employee();
employee.setEmployeeId(1001);
employee.setEmployeeName("John Doe");
employee.setAddresses(Arrays.asList(new Address("state_name", "country_name")));
userRepository.save(employee);
}
}
地址的数据类型是 Postgres 中的文本。
在执行我的代码时,我收到以下异常:
org.postgresql.util.PSQLException: ERROR: 关系“employee_address”不存在。
【问题讨论】:
-
尝试使用 oneToMany 和 ManyToOne 等注释 like the solution here
-
我的地址 DTO 不是表,它是一个普通的 POJO。
标签: java postgresql spring-boot jpa