Using JPA annotations and hibernation, we recently ran into the onetomany unidirectional display problem, which looked like this:
@Entity public class FooOrder extends AbstractEntity{ @OneToMany(cascade= CascadeType.ALL,orphanRemoval = true) @JoinColumn(name = "fooOrderId") private List<FooItem> fooItems = new ArrayList<FooItem>(); public void addFooItem(foo item properties here) { fooItems.add(fooItem); } } @Entity public class FooItem extends AbstractEntity { SomeRandomStuffButNoLinkToParent }
Basically this test code:
FooOrder fooOrder = new FooOrder(stuff here); fooOrder.addFooItem(foo item properties here); fooOrder = fooOrderRepository.save(fooOrder);
When we run the tests, we get sql, which looks something like this:
insert FooOrder(columns here) insert FooItem(columns here missing the FK to FooOrder) update FooItem set FooOrderFK to proper key.
but if I set @JoinColumn(name = "activeOrderId", nullable = false) , then my sql looks something like this:
insert FooOrder(columns here) insert FooItem(columns here with FK to FooOrder)
Why does hibernate install FK through an update if it is NULL but sets it in the insert when it cannot be null?
source share