I am developing a Rest API which will receive a JSON. I need to save the JSON correctly in a Postgres DB.
Right now I have:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "customer", cascade = ALL)
private List<Address> address;
}
And
@Entity
public class Address {
@Id
@GeneratedValue(strategy = AUTO)
private Long id;
private String city;
private String number;
private String country;
@ManyToOne
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;
}
My controller has only this:
@RequestMapping(method = POST)
public ResponseEntity create(@RequestBody Customer customer) {
Customer customerSaved = repository.save(customer);
return new ResponseEntity(customerSaved, CREATED);
}
When I send a JSON like:
{
"name":"A name",
"address":[
{
"country":"Brazil",
"city":"Porto Alegre",
"number":"000"
}
]
}
I was expecting that the table Customer would have the name and table Address would have the three properties plus the customer_id. But the customer_id right now is null.
Any thoughts?