Problem with NHibernate
Posted
by Bernard Larouche
on Stack Overflow
See other posts from Stack Overflow
or by Bernard Larouche
Published on 2010-04-16T04:53:05Z
Indexed on
2010/04/16
5:53 UTC
Read the original article
Hit count: 293
I am trying to get a list of Products that share the Category.
NHibernate returns no product which is wrong.
Here is my Criteria API method :
public IList<Product> GetProductForCategory(string name)
{
return _session.CreateCriteria(typeof(Product))
.CreateCriteria("Categories")
.Add(Restrictions.Eq("Name", name))
.List<Product>();
}
Here is my HQL method :
public IList<Product> GetProductForCategory(string name)
{
return _session.CreateQuery("select from Product p, p.Categories.elements c where c.Name = :name").SetString("name",name).List<Product>();
}
Both methods return no product when they should return 2 products.
Here is the Mapping for the Product class :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CBL.CoderForTraders.DomainModel" namespace="CBL.CoderForTraders.DomainModel">
<class name="Product" table="Products" >
<id name="_persistenceId" column="ProductId" type="Guid" access="field" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="assigned" />
</id>
<version name="_persistenceVersion" column="RowVersion" access="field" type="int" unsaved-value="0" />
<property name="Name" column="ProductName" type="String" not-null="true"/>
<property name="Price" column="BasePrice" type="Decimal" not-null="true" />
<property name="IsTaxable" column="IsTaxable" type="Boolean" not-null="true" />
<property name="DefaultImage" column="DefaultImageFile" type="String"/>
<bag name="Descriptors" table="ProductDescriptors">
<key column="ProductId" foreign-key="FK_Product_Descriptors"/>
<one-to-many class="Descriptor"/>
</bag>
<bag name="Categories" table="Categories_Products" >
<key column="ProductId" foreign-key="FK_Products_Categories"/>
<many-to-many class="Category" column="CategoryId"></many-to-many>
</bag>
<bag name="Orders" generic="true" table="OrderProduct" >
<key column="ProductId" foreign-key="FK_Products_Orders"/>
<many-to-many column="OrderId" class="Order" />
</bag>
</class>
</hibernate-mapping>
And finally the mapping for the Category class :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CBL.CoderForTraders.DomainModel" namespace="CBL.CoderForTraders.DomainModel" default-access="field.camelcase-underscore" default-lazy="true">
<class name="Category" table="Categories" >
<id name="_persistenceId" column="CategoryId" type="Guid" access="field" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="assigned" />
</id>
<version name="_persistenceVersion" column="RowVersion" access="field" type="int" unsaved-value="0" />
<property name="Name" column="Name" type="String" not-null="true"/>
<property name="IsDefault" column="IsDefault" type="Boolean" not-null="true" />
<property name="Description" column="Description" type="String" not-null="true" />
<many-to-one name="Parent" column="ParentID"></many-to-one>
<bag name="SubCategories" inverse="true">
<key column="ParentID" foreign-key="FK_Category_ParentCategory" />
<one-to-many class="Category"/>
</bag>
<bag name="Products" table="Categories_Products">
<key column="CategoryId" foreign-key="FK_Categories_Products" />
<many-to-many column="ProductId" class="Product"></many-to-many>
</bag>
</class>
</hibernate-mapping>
Can you see what could be the problem ?
© Stack Overflow or respective owner