all.
I'm currently facing a challenge in tackling a straightforward issue. Within a Spring Boot application, I have two entities integrated with a web RESTful interface. While I can seamlessly insert and select rows that are standalone without dependencies on other tables, I encounter issues when attempting to insert a row with a foreign key. The row gets inserted, but the ID linking it to the parent table fails to be assigned correctly.
The parent table, Customer:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstname;
private String mi;
private String lastname;
...}
The child table, Invoice:
@Entity
@Table(name = "INVOICE")
public class Invoice {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private Date dropoff;
private Date ready;
private String note;
private Boolean paid;
private BigDecimal total_price;
private long total_quantity;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="CUSTOMER_ID")
private Customer customer;
...}
Below are the POST tests I've experimented with sending to http://localhost:8080/invoices
POST Test 1:
{
"dropoff":"2017-01-30 10:33",
"ready":"",
"paid":"false",
"note":"manual insert",
"customer_id":"1"
}
POST Test 2:
{
"dropoff":"2017-01-30 10:33",
"ready":"",
"paid":"false",
"note":"manual insert",
"customer":{
"id":"1"
}
}
POST Test 3:
{
"dropoff":"2017-01-30 10:33",
"ready":"",
"paid":"false",
"note":"manual insert",
"customerid":"1"
}
POST Test 4:
{
"dropoff":"2017-01-30 10:33",
"ready":"",
"paid":"false",
"note":"manual insert",
"customerId":"1"
}
Although the data appears when I execute a select query on the Invoice table, it doesn't seem to be associated with the customer ID of 1.
An intriguing observation: When I experiment with POST example 2, the invoice record with ID 1 gets updated. It seems to disregard the customer object within the payload.
I am uncertain about the correct format to follow in order to link an invoice to a customer with ID 1. So far, all attempts result in successful insertion, but without establishing any foreign key connection to the Customer table.