Hello,
Sorry if the topic was already posted but I didn't find any help for my problem.
I have two entities "Piece" and "Ligne" and would like to query them with criteria API. As you can see in the following mapping files between "Piece" and "Ligne" there is a one-to-many relation.
I would like to get some "Piece" that IDPIECE = "anID" and "Lignes" that QTERESTEALIVRER property >0. When I query on the piece property IDPIECE = "anID" all works good but if I also query on "Ligne" property QTERESTEALIVRER, I have the following error "could not resolve property" there is no QTERESTEALIVRER property in the "Piece" class. How to tell NHIBERNATE that QTERESTEALIVRER is in "Ligne" class and that I want all pieces with IDPIECE="anID" with "Lignes" which QTERESTEALIVRER > 0 ? Why Criteria API doesn't make a link itself from mapping files? Here is the mapping files and some code.
Piece mapping :
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="CommandesModel"
assembly="CommandesModel">
<!-- Mappings for class 'ZPiece' -->
<class name="Piece" table="ZPIECE" lazy="false">
<!-- Identity mapping -->
<id name="IDPIECE">
<column name="ZP_IDPIECE" />
<generator class="assigned" />
</id>
<version column="ZP_VERSION" name="VERSION" type="Int32" access="property" unsaved-value="0" generated="never" />
<!-- One-to-many mapping: Lignes -->
<bag name="Lignes" cascade="all-delete-orphan" lazy="false">
<key column="ZL_IDPIECE" />
<one-to-many class="Ligne" />
</bag>
<!-- Simple mappings -->
<property name="NATUREPIECEG" column="ZP_NATUREPIECEG" />
<property name="NUMERO" column="ZP_NUMERO" />
<property name="SOUCHE" column="ZP_SOUCHE" />
<property name="REFINTERNE" column="ZP_REFINTERNE" />
<property name="REFEXTERNE" column="ZP_REFEXTERNE" />
<property name="TIERS" column="ZP_TIERS" />
<property name="TIERSLIVRE" column="ZP_TIERSLIVRE" />
<property name="TIERSFACTURE" column="ZP_TIERSFACTURE" />
<property name="REPRESENTANT" column="ZP_REPRESENTANT" />
</class>
</hibernate-mapping>
Ligne mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="CommandesModel"
assembly="CommandesModel">
<!-- Mappings for class 'Ligne' -->
<class name="Ligne" table="ZLIGNE" lazy="false">
<!-- Identity mapping -->
<id name="IDLIGNE">
<column name="ZL_IDLIGNE" not-null="true" />
<generator class="assigned" />
</id>
<version column="ZL_VERSION" name="VERSION" type="Int32" access="property" unsaved-value="0" generated="never" />
<many-to-one name="Piece" class="Piece" cascade="all">
<column name="ZL_IDPIECE" not-null="true" />
</many-to-one>
<property name="NATUREPIECEG" column="ZL_NATUREPIECEG" />
<property name="NUMERO" column="ZL_NUMERO" />
<property name="QTERESTEALIVRER" column="ZL_QTERESTEALIVRER" />
</class>
</hibernate-mapping>
Here is what I try to do:
Code:
using (ISession session = m_SessionFactory.OpenSession())
{
// Create a criteria object with the specified criteria
ICriteria criteria = session.CreateCriteria(typeof(Piece));
criteria.Add(Expression.Eq("IDPIECE", "anID"));
criteria.Add(Expression.Gt("QTERESTEALIVRER", 0));
// Get the matching objects
IList<Piece> matchingObjects = criteria.List<Piece>();
}
But the following doesn't work
Code:
using (ISession session = m_SessionFactory.OpenSession())
{
// Create a criteria object with the specified criteria
ICriteria criteria = session.CreateCriteria(typeof(Piece));
criteria.Add(Expression.Eq("IDPIECE", "anID"));
criteria.CreateCriteria("Lignes").Add(Expression.Gt("QTERESTEALIVRER", 0));
// Get the matching objects
IList<Piece> matchingObjects = criteria.List<Piece>();
}
Thanks for your help in advance.
Thomas
Hibernate version: 2.0.50727