Hello, I am using NHibernate 1.2 to construct a proof of concept project as well as a learning example. I have the following problem in my Domain Objects where.
Code:
public class Customer {
public virtual int CustomerId;
}
public class CustomerAtSite : Customer {
public virtual int CustomerAtSite;
public virtual IList<AssignedProduct> Products;
}
public class Product {
public virtual int ProductId;
}
public class AssignedProduct : Product {
public virtual int AssignedProductId;
public virtual CustomerAtSite Customer;
}
Where in the database you have the following tables:
CUSTOMER with pk CUSTOMER_ID
CUSTOMER_SITES with pk CUSTOMER_SITE_ID and fk CUSTOMER_ID
PRODUCT with pk PRODUCT_ID
CUST_PROD with pk CUST_PROD_ID and fk PROD_ID for inheritance, and fk CUSTOMER_SITE_ID for a one-to-many reference
In addition, I have the following hbms declared:
Customer.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="EDC2_Objects.Core.Domain.Customer, EDC2_Objects.Core" table="Customer" lazy="false">
<id name="CustomerId" column="CUSTOMER_ID" type="Int32">
<generator class="native" />
</id>
<joined-subclass name="EDC2_Objects.Core.Domain.CustomerAtSite, EDC2_Objects.Core" table="CUSTOMER_SITES">
<key column="CUSTOMER_ID" />
<property name="CustomerSiteId" column="CUSTOMER_SITE_ID" type="Int32" not-null="true" unique-key="true"/>
<bag name="Products" lazy="true">
<key column="CUSTOMER_SITE_ID" foreign-key="CustomerSiteId"/>
<one-to-many class="EDC2_Objects.Core.Domain.AssignedProduct, EDC2_Objects.Core" />
</bag>
</joined-subclass>
</class>
</hibernate-mapping>
And, Product.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="EDC2_Objects.Core.Domain.Product, EDC2_Objects.Core" table="Product" lazy="false">
<id name="ProductId" column="PROD_ID" type="Int32">
<generator class="native" />
</id>
<property name="Name" column="NAME" type="String" length="50" not-null="false"/>
<joined-subclass name="EDC2_Objects.Core.Domain.AssignedProduct, EDC2_Objects.Core" table="CUST_PROD">
<key column="PROD_ID" />
<property name="CustomerProductId" column="CUST_PROD_ID" type="Int32" not-null="true" unique-key="true" />
<many-to-one name="Customer" column="CUSTOMER_SITE_ID" not-null="true" class="EDC2_Objects.Core.Domain.CustomerAtSite, EDC2_Objects.Core" property-ref="CustomerSiteId"/>
</joined-subclass>
</class>
</hibernate-mapping>
Finally, I am testing it with the following piece in MbUnit:
Code:
[TestFixture]
[FixtureCategory("Database")]
public class CustomerAtSiteTests : NHibernateTestCase {
[Test]
public void CustomerHasProducts() {
int cs_id = 54;
int[] e_cp_id = {116, 120};
IDaoFactory factory = new NHibernateDaoFactory();
ICustomerAtSiteDao dao = factory.GetCustomerAtSiteDao();
CustomerAtSite c = dao.GetById(cs_id);
Assert.AreEqual(e_cp_id.Length, c.Products.Count);
foreach (AssignedProduct p in c.Products) {
CollectionAssert.Contains(e_cp_id, p.ProductId);
Assert.AreEqual(c.CustomerSiteId, p.Customer.CustomerSiteId);
}
}
}
The test fails, and examining the the NHibernate show_sql=true output, I see that that call to c.Products executes as:
Code:
SELECT products0_.CUSTOMER_SITE_ID AS CUSTOMER3___1_, products0_.PROD_ID AS PROD1_1_, products0_.PROD_ID AS PROD1_5_0_, products0_.CUST_PROD_ID AS CUST2_6_0_,
products0_.CUSTOMER_SITE_ID AS CUSTOMER3_6_0_, products0_1_.NAME AS NAME5_0_
FROM CUST_PROD products0_ INNER JOIN Product products0_1_ ON products0_.PROD_ID = products0_1_.PROD_ID
WHERE products0_.CUSTOMER_SITE_ID =:p0; :p0 = '46'
The problem is visible immediately. 46 is the CUSTOMER_ID but p0 should be se to the value of CUSTOMER_SITE_ID (:p0=54);
Can anyone help correct my mapping?