hbm2ddl doesn't create foreign key on generator="foreign".

Description

I create two hbm.xml, and generate ddl by SchemaExport.
---------------begin CProduct.hbm.xml ------------------------ <?xml version="1.0" encoding="gb2312" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="com.easieroa.jxc3.data.Base.CProduct, jxc3.data" table="t_product" >
<id name="Id" type="Int32" column="fid" unsaved-value="0">
<generator class="sequence" >
<param name="sequence">seq_t_product_id</param>
</generator>
</id>
<version name="Version" column="fversion"/>
<property name="ProductCode" type="String" column="fproduct_code" length="20" not-null="true" unique="true" />
<property name="ProductName" type="String" column="fproduct_name" length="40" not-null="true" />
<property name="ProductShortName" type="String" column="fproduct_short_name" length="20" />
<property name="PYCode" type="String" column="fpy_code" length="40" />
<property name="Origin" type="String" column="forgin" length="60" />
<property name="Manufacture" type="String" column="fmanufacture" length="60" />
<property name="Model" type="String" column="fmodel" length="60" />
<property name="Valid" type="yes_no" column="fvalid" length="1" />
<many-to-one name="Category" column="fcat_id" />
<many-to-one name="Unit" column="funit_id"/>
<one-to-one name="Detail" class="com.easieroa.jxc3.data.Base.CProductDetail, jxc3.data" />
</class>
</hibernate-mapping>
---------------end CProduct.hbm.xml ------------------------
---------------begin CProductDetail.hbm.xml ------------------------ <?xml version="1.0" encoding="gb2312" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="com.easieroa.jxc3.data.Base.CProductDetail, jxc3.data" table="t_product_detail" >
<id name="Id" type="Int32" column="fid" unsaved-value="0">
<generator class="foreign" >
<param name="property">Product</param>
</generator>
</id>
<version name="Version" column="fversion"/>
<one-to-one name="Product" />
<property name="ReferencePurchasePrice" type="Decimal" column="freference_purchase_price" length="3" />
<property name="SellingPrice1" type="Decimal" column="fselling_price1" length="3" />
<property name="SellingPrice2" type="Decimal" column="fselling_price2" length="3" />
<property name="SellingPrice3" type="Decimal" column="fselling_price3" length="3" />
<property name="SellingPrice4" type="Decimal" column="fselling_price4" length="3" />
<property name="LastPurchasePrice" type="Decimal" column="flast_purchase_price" length="3" />
<property name="TotalStock" type="Decimal" column="ftotal_stock" length="3" />
<property name="CostPrice" type="Decimal" column="fcost_price" length="3" />
</class>
</hibernate-mapping>
---------------end CProductDetail.hbm.xml ------------------------
the SchemaExport should generate an foreign key constraint on the table t_product_detail like this:
alter table t_product_detail add constraint FKA12508E57C842222 foreign key (fid) references t_product

but,in fact, the SchemaExport doesn't generate it!

Environment

.Net 1.1 , NHibernate 0.7Beta

Activity

Show:

Former user 
May 12, 2005 at 10:47 AM

Closing this as "not-a-bug".

Luca Altea 
April 11, 2005 at 4:10 PM

The problem is not in the NHibernate code but in CProductDetail.hbm.xml

A <generator class="foreign" /> node is linked through the parameter "property" to the appropriate <one-to-one> node, in this case the "Product" <one-to-one> node in the CProductDetail.hbm.xml file, by the NHibernate.Cfg.Binder.MakeIdentifier function.

A foreign key is generated by the SchemaExport only if the corresponding <one-to-one> node has the "constrained" attribute set to "true".

The Hibernate documentation for the <one-to-one> tag states:

"constrained (optional) specifies that a foreign key constraint on the primary key of the mapped table references the table of the associated class. This option affects the order in which save() and delete() are cascaded (and is also used by the schema export tool)."

The CProductDetail.hbm.xml should be edited as follows:

<one-to-one name="Product" constrained="true" />

and SchemaExport will generate the appropriate foreign key in the SQL DDL.

Not an Issue

Details

Assignee

Reporter

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created February 27, 2005 at 8:58 AM
Updated May 12, 2005 at 10:47 AM
Resolved May 12, 2005 at 10:47 AM
Who's Looking?