@OneToOne(optional = false, ...) allows to save object with a null referenc
I have a simple mapping: Category has a CategoryInfo reference. This is a one-to-one, not optional, and in one direction only. The database schema doesn't impose the not-null constraint on the foreign key.
<br /> @OneToOne(fetch = FetchType.EAGER, optional = false)<br /> @JoinColumn(name = "CATEGORY_INFO_ID")<br /> private CategoryInfo info;<br />
In the code, I create a new Category, and never assign a CategoryInfo to it, this is null, but the code runs fine, and the object is saved to the database. When I enable a not-null constraint, JDBC reports an error.
When I switch the provider from TopLink to Hibernate, and disable the not-null constraint again, running the same code results in an exception, which says that a not-null reference is null, which is what I expected.
So, there is a difference in behaviour between two providers. Could anyone tell me if this is allowed? Or maybe the optional attribute can be ignored by the provider?
I went here:
and this description lacks the "optional" attribute at all, hence my question if this is optional or not.