1

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

Orkun
  • 6,998
  • 8
  • 56
  • 103
  • As you have a bi-directional relationship you have to make sure you set both fields. You probably only set the `InvoiceSequence` on the `Invoice` but not the other way around. – M. Deinum Apr 18 '18 at 14:13
  • Can you share the method that you use to insert Invoice and how you populate the `Invoice` and `InvoiceSequence` variables ? – Al-Mustafa Azhari Apr 18 '18 at 14:21
  • @Zeromus i ve removed the annotations that might be irrelevant.. – Orkun Apr 18 '18 at 14:31
  • @Al-MustafaAzhari i m testing via unit tests against h2 db.. i wonder if it would be different with a real mysql. – Orkun Apr 18 '18 at 14:31
  • Did you create InvoiceSequence object and set in Invoice object? – Elias Apr 18 '18 at 15:16
  • @ShaikElias No. But thanks, indeed that s practically my question: i d like it to be done automagically by hibernate/jpa. is it possible? – Orkun Apr 18 '18 at 15:30
  • Try to set empty object of `InvoiceSequence` to invoice , like `invoice.setInvoiceSequence(new InvoiceSequence());` before call the `invoiceService.save(invoice);` mehtod – Al-Mustafa Azhari Apr 18 '18 at 15:35

1 Answers1

4

This seems a wrong way of implementing a sequence. Why don't you have another field in Invoice like sequence and generate it using something like this:

Multiple Hibernate sequence generators for one entity with PostgreSQL

Also, if you really want to do it the way you suggested you need to manually create InvoiceSequence and then set it to Invoice before saving. You also need to set the Invoice to InvoiceSequence as described here:

https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-one-mapping-example/

humbaba
  • 318
  • 2
  • 10
  • thanks! we are using MariaDb but i know we can do the same as in option#1. I went for option#2. – Orkun Apr 19 '18 at 06:43