These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: General question about Criteria API
PostPosted: Mon Aug 28, 2006 7:03 am 
Beginner
Beginner

Joined: Tue Mar 28, 2006 3:44 am
Posts: 22
Hi all :)

I have a general question about the Criteria API.
According to the NHibernate reference it is possible to navigate associations like this...
Code:
IList cats = session.CreateCriteria(typeof(Cat))
    .CreateCriteria("kittens")
        .Add( Expression.like("name", "Iz%") )
        .List();


Now my question. Is it also possible to navigate through a more complicated association path? Here is an example:
Code:
Item
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Data.Dto.ItemDto,Data.Dto" table="ITEM" lazy="false">
      <id name="ItemId" column="ITEM_ID" type="Int32" access="field.lowercase" unsaved-value="0">
         <generator class="native"/>
      </id>
      <many-to-one column="BUID" name="Bu" class="Data.Dto.BusinessUnitDto,Data.Dto" access="field.lowercase" />
      <many-to-one column="ITEM_TYPE_ID" name="ItemType" class="Data.Dto.ItemTypeDto,Data.Dto" access="field.lowercase" />
      <property column="ITEM_NAME" type="String" access="field.lowercase" name="ItemName" not-null="true" length="50" />
      <property column="INTERNAL_KEY" type="String" access="field.lowercase" name="InternalKey" length="25" />
      <property column="ITEM_DESCRIPTION" type="String" access="field.lowercase" name="ItemDescription" length="100" />
      <many-to-one column="MANUFACTURER_ID" name="Manufacturer" class="Data.Dto.ManufacturerDto,Data.Dto" access="field.lowercase" />
      <property column="HEIGHT" type="Decimal" access="field.lowercase" name="Height" not-null="true" />
   </class>
</hibernate-mapping>

"Relationship-table" (btw what is the right term for such a table in English?) between property value and item to resolve the m:n relationship
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Data.Dto.ItemPropertyRelationDto,Data.Dto" table="ITEM_PROPERTY_RELATION" lazy="true">
      <id name="ItemPropertyRelationId" column="ITEM_PROPERTY_RELATION_ID" type="Int32" access="field.lowercase" unsaved-value="0">
         <generator class="native"/>
      </id>
      <many-to-one column="PROPERTY_VALUE_ID" name="PropertyValue" class="Data.Dto.PropertyValueDto,Data.Dto" access="field.lowercase" />
      <many-to-one column="ITEM_ID" name="Item" class="Data.Dto.ItemDto,Data.Dto" access="field.lowercase" />
      <property column="IS_ACTIVE" type="Boolean" access="field.lowercase" name="IsActive" not-null="true" />
   </class>
</hibernate-mapping>

Property value
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Data.Dto.PropertyValueDto,Data.Dto" table="PROPERTY_VALUE" lazy="true">
      <id name="PropertyValueId" column="PROPERTY_VALUE_ID" type="Int32" access="field.lowercase" unsaved-value="0">
         <generator class="native"/>
      </id>
      <many-to-one column="PROPERTY_ID" name="Property" class="Data.Dto.PropertyDto,Data.Dto" access="field.lowercase" />
      <property column="PROPERTY_VALUE" type="String" access="field.lowercase" name="PropertyValue" not-null="true" length="50" />
      <property column="IS_ACTIVE" type="Boolean" access="field.lowercase" name="IsActive" not-null="true" />
   </class>
</hibernate-mapping>

Manufacturer
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Data.Dto.ManufacturerDto,Data.Dto" table="MANUFACTURER" lazy="true">
      <id name="ManufacturerId" column="MANUFACTURER_ID" type="Int32" access="field.lowercase" unsaved-value="0">
         <generator class="native"/>
      </id>
      <many-to-one column="BUID" name="Bu" class="Data.Dto.BusinessUnitDto,Data.Dto" access="field.lowercase" />
      <property column="MANUFACTURER_NAME" type="String" access="field.lowercase" name="ManufacturerName" not-null="true" length="50" />
      <property column="ILN" type="String" access="field.lowercase" name="Iln" length="25" />
      <property column="INTERNAL_KEY" type="String" access="field.lowercase" name="InternalKey" length="25" />
      <many-to-one column="COUNTRY_ID" name="Country" class="Data.Dto.CountryDto,Data.Dto" access="field.lowercase" />
   </class>
</hibernate-mapping>


I skipped some unimportend mapping information. As you can imagine I have to select the correct items using e.g. a manufacturer name and a property value. Navigation through the associations looks like this: Manufacturer <- Item <- "Relationship-table Item-Property" -> Property Value.
Is it possible to build such a more complex case with the Criteria API? So far I didn't find a way to solve this problem without HQL.

Many thanks in advance
Chavez


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.