【问题标题】:How to save List of objects in Postgres in springboot application如何在 Spring Boot 应用程序中保存 Postgres 中的对象列表
【发布时间】: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


【解决方案1】:

UserRepository 是否扩展了 org.springframework.data.repository.CrudRepository?如果是这样... Spring 存储库不能使用普通的 DTO 运行。 Spring 存储库只能保存实体(类应使用 @Entity 注释)。

因此,您可以使用 @Entity 注释 EmployeeAddress 类。在这种情况下,您还需要将@OneToMany 用于Employee#addresses。您还需要指定@Id 属性。

    @Entity
    public class Employee {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer id;
        private Integer employeeId;
        private String employeeName;
        @OneToMany
        private List<Address> addresses;

        // getters + setters + eq/hc
    }

    @Entity
    public class Address {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer id;
        private String state;
        private String country;

        // getters + setters + eq/hc
    }

您可以在以下指南中找到示例:https://spring.io/guides/gs/accessing-data-jpa/

【讨论】:

  • 感谢您的回答。地址类只是一个 DTO,它不是一个实体。
【解决方案2】:

您可以在地址 DTO 上使用 @ElementCollection 以上的 List&lt;Address&gt; addresses@Embeddable

【讨论】:

    猜你喜欢
    • 2016-05-01
    • 2023-03-29
    • 2017-12-11
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2019-11-01
    相关资源
    最近更新 更多