I have created a OneToOne relationship as in the post : https://stackoverflow.com/a/536102/494659
Similarly we have two entities: Invoice
& InvoiceSequence
which is basically a sequence for it.
@Entity
@Table(name = "invoices", indexes = {...})
@JsonIgnoreProperties(ignoreUnknown = true)
public class Invoice implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Id
private String id;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "invoice")
private InvoiceSequence invoiceSequence;
...
}
and the InvoiceSequence entity:
@Entity
public class InvoiceSequence {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long seqId;
@OneToOne
private Invoice invoice;
@Override
public String toString() {
return new ToStringBuilder(this)
.append("seqId", seqId)
.toString();
}
}
But when we save the Invoice, InvoiceSequence is saved as null. Example, in my unit test;
Invoice invoice = new Invoice();
invoice.setFleetId("F0001");
invoice.setInvoicesStatus(InvoicesStatus.PENDING);
invoice.setPricingId("1V1");
invoice.setInvoiceDate(Date.from(toInstant));
invoice.setTripId("000011111");
invoice.setServiceId("001111");
invoice.setUserId("1");
invoice.setAmount(new BigDecimal("56"));
invoiceService.save(invoice);
when i debug and look in the (in memory, H2) db, i see that the invoiceSequence is null.
The problem is very simple : How can I make sure we have an incremented InvoiceSequence in long value when we insert a new Invoice?
but I cannot get it work, any ideas what I am doing wrong?
Thanks