persistence@glassfish.java.net

Any foreign keys with Toplink and Derby ?

From: Antonio Goncalves <antonio.mailing_at_gmail.com>
Date: Tue, 26 Sep 2006 15:17:05 +0200

Hi all,

I'm running on Glassfish b48 a petstore like application with 3 entity
beans : Categories <------*-> Product <-------*-> Item (all relations are
bi-directional). When the DDL file that creates the tables is generated I
can't see any foreign key. I've also use Squirrel to make sure nothing was
created on my back and it looks like no constraint is defined.

Is there a way to have foreign keys with Toplink and Derby ?

Thanks,

Antonio


Entity Beans (simpplified)
===================
@Entity
@Table(name = "t_category")
public class Category implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    @Column(nullable = false, length = 30)
    private String name;
    @Column(nullable = false)
    private String description;
    @OneToMany(mappedBy = "category", cascade = CascadeType.REMOVE)
    @OrderBy("name ASC")
    private List<Product> products;

}

@Entity
@Table(name = "t_product")
public class Product implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    @Column(nullable = false, length = 30)
    private String name;
    @Column(nullable = false)
    private String description;
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "category_fk")
    private Category category;
    @OneToMany(mappedBy = "product", cascade = CascadeType.REMOVE)
    @OrderBy("name ASC")
    private List<Item> items;

}

@Entity
@Table(name = "t_item")
public class Item implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    @Column(nullable = false, length = 30)
    private String name;
    @Column(name = "unit_cost", nullable = false)
    private Float unitCost;
    @Column(name = "image_path")
    private String imagePath;
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "product_fk")
    private Product product;
}

Persistence.xml file
==============
    <persistence-unit name="petstorePU">
        <jta-data-source>jdbc/petstoreDS</jta-data-source>
        <properties>
            <property name="toplink.platform.class.name"
                      value="
oracle.toplink.essentials.platform.database.DerbyPlatform"/>
            <property name="toplink.ddl-generation"
value="drop-and-create-tables"/>
            <property name="toplink.create-ddl-jdbc-file-name" value="
create.sql"/>
            <property name="toplink.drop-ddl-jdbc-file-name" value="drop.sql
"/>
        </properties>
    </persistence-unit>
</persistence>


create.sql DDL
===========
CREATE TABLE t_product (ID BIGINT NOT NULL, DESCRIPTION VARCHAR(255) NOT
NULL, NAME VARCHAR(30) NOT NULL, category_fk BIGINT, PRIMARY KEY (ID))

CREATE TABLE t_category (ID BIGINT NOT NULL, NAME VARCHAR(30) NOT NULL,
DESCRIPTION VARCHAR(255) NOT NULL, PRIMARY KEY (ID))

CREATE TABLE t_item (ID BIGINT NOT NULL, unit_cost FLOAT NOT NULL,
image_path VARCHAR(255), NAME VARCHAR(30) NOT NULL, product_fk BIGINT,
PRIMARY KEY (ID))