Hi Antonio,
You can use @JoinColumn(nullable=false) for your pupose.
These two look similar and confusing, but I think the strict meaning of the
optional() element and nullable() element are different. The former is a
logical one which means that the field of the entity object can be null or
not, but the latter one has a physical meaning that the corresponding
database column can be nullable and this is used to generate DDLs.
optional() value may be used by a provider to check the field before
synchronizing to database.
What I know is that current implementation doesn't utilize the optional()
value - I'm not sure this a hint (@Basic specifies this as a hint). If this
is just a hint, users should not rely on it.
If this kind of validation is required in TopLink Essentials, filing an
enhacement issue is a good idea.
-Wonseok
On 11/5/06, Antonio Goncalves <antonio.mailing_at_gmail.com> wrote:
>
> Hi,
>
> I have the following example that models a customer that has one and only
> one delivery adress (1-1) and an optional home address (0--1). To make the
> difference between 1-1 and 0-1 I use the optional attribute of the OneToOne
> annotation. Here is the code :
>
>
> @Entity
> public class Client {
>
> @Id
> @GeneratedValue
> private Long id;
> @OneToOne(*optional = true*)
> private Address homeAddress;
> @OneToOne(*optional = false*)
> private Address deliveryAddress;
>
> }
>
> @Entity
> public class Address {
>
> @Id
> @GeneratedValue
> private Long id;
> private String street1;
> private String street2;
>
> }
>
> When the DDL is generated there is no Not Null for the deliveryAddress
> column. In the spec for the OneToOne annotation (§ 9.1.23) you can read :
> optional = Whether the association is optional. If set to false then a
> non-null relationship must always exist.
>
> CREATE TABLE CLIENT (ID BIGINT NOT NULL, *HOMEADDRESS_ID* BIGINT, *DELIVERYADDRESS_ID
> *BIGINT, PRIMARY KEY (ID))
> ALTER TABLE CLIENT ADD CONSTRAINT CLIENTHMEADDRESSID FOREIGN KEY
> (HOMEADDRESS_ID) REFERENCES ADDRESS (ID)
> ALTER TABLE CLIENT ADD CONSTRAINT CLNTDLVRYADDRESSID FOREIGN KEY
> (DELIVERYADDRESS_ID) REFERENCES ADDRESS (ID)
>
> Have I missed something or is that a bug ?
>
> Thanks,
>
> Antonio
>