users@glassfish.java.net

_at_OneToOne(optional = false, ...) allows to save object with a null referenc

From: <glassfish_at_javadesktop.org>
Date: Thu, 20 Nov 2008 14:12:30 PST

Hi.
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.

[code]
@OneToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "CATEGORY_INFO_ID")
private CategoryInfo info;
[/code]

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:
http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#OneToOne
and this description lacks the "optional" attribute at all, hence my question if this is optional or not.

Thank you.
[Message sent by forum member 'szczyp' (szczyp)]

http://forums.java.net/jive/thread.jspa?messageID=317957