persistence@glassfish.java.net

Not null OneToOne relation

From: Antonio Goncalves <antonio.mailing_at_gmail.com>
Date: Sun, 5 Nov 2006 15:32:40 +0100

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