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.

asked Nov 10, 2019 at 14:12
2
  • Try with annotations like oneToMany and ManyToOne like the solution here Commented Nov 10, 2019 at 16:00
  • My Address DTO is not a table, it's a plain POJO. Commented Nov 10, 2019 at 16:53

2 Answers 2

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/

answered Nov 10, 2019 at 17:47
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. Address class is just a DTO, it's not an entity.
0

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

Syscall
19.8k10 gold badges44 silver badges60 bronze badges
answered Mar 31, 2021 at 11:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.