1

I am working on a springboot application.
I am trying to save a list of custom objects in one of the column, but I am getting ERROR: relation doesn't exist exception on one column

EMPLOYEE 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;
}



}



ADDRESS 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() {
}
}



Employee Service:

@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);
}

}



Data type of address is text in Postgres.



While executing my code, I am getting the below Exception:
org.postgresql.util.PSQLException: ERROR: relation “employee_address” does not exist.

mayank bisht
  • 618
  • 3
  • 14
  • 43
  • Try with annotations like oneToMany and ManyToOne [like the solution here](https://stackoverflow.com/questions/52416970/onetomany-manytoone-mapping-spring-boot-hibernate) – Apophis Nov 10 '19 at 16:00
  • My Address DTO is not a table, it's a plain POJO. – mayank bisht Nov 10 '19 at 16:53

2 Answers2

1

Does UserRepository extend org.springframework.data.repository.CrudRepository? If so... Spring repositories can't operate with plain DTO. Spring repositories can save only entities (class should be annotated with @Entity).

So, you can annotate Employee and Address classes with @Entity. In this case you also need to use @OneToMany for Employee#addresses. You also need to specify @Id property.

    @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
    }

You can find an example in the following guide: https://spring.io/guides/gs/accessing-data-jpa/

Petr Aleksandrov
  • 1,434
  • 9
  • 24
0

You can use @ElementCollection above List<Address> addresses and @Embeddable on Address DTO.

Syscall
  • 19,327
  • 10
  • 37
  • 52