-->
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.  [ 4 posts ] 
Author Message
 Post subject: Nhibernate CreateCriteria
PostPosted: Thu Sep 10, 2009 6:02 am 
Newbie

Joined: Thu Aug 13, 2009 3:52 pm
Posts: 4
Hi,

I have a 2 classes: product and contact

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <!--Build: with lujan99@usa.net Nhibernate template-->
  <class name="Kiwa.Objects.Product,Kiwa.Objects" table="Product" lazy="true">
    <id name="Id" column="ID" type="int">
      <generator class="native" />
    </id>
    <property name="Productnr" column="ProductNR" type="string" />
    <many-to-one name="Contact1" column="ContactID1" cascade="save-update" />
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <!--Build: with lujan99@usa.net Nhibernate template-->
  <class name="Kiwa.Objects.ProductManager,Kiwa.Objects" table="ProductManager" lazy="true">
    <id name="Id" column="ID" type="int">
      <generator class="native" />
    </id>
    <property name="Name" column="Name" type="string" not-null="true" />
    <property name="Email" column="Email" type="string" />
    <property name="Telephone" column="Telephone" type="string" />
    <bag name="Contact1Product" inverse="true" lazy="true" cascade="delete">
      <key column="ContactID1" />
      <one-to-many class="Kiwa.Objects.Product,Kiwa.Objects" />
    </bag>
  </class>
</hibernate-mapping>


No I want te create a query that gives me all products where the contactName is "Piet".
I Tried the following code:
Code:
Dim session As ISession = SessionHelper.CurrentSession()

session.CreateCriteria(GetType(Product)).SetCacheable(False) _
    .Add(Expression.Like("Contact1.Name", "Piet")) _
.List(Of Product)()


This gives me the following error: could not resolve property: Contact1.Name of: Kiwa.Objects.Product

When I do the same statment but with Contact.Id it works fine.

Does someone know what i'm doing wrong?

Thx.


Top
 Profile  
 
 Post subject: Re: Nhibernate CreateCriteria
PostPosted: Thu Sep 10, 2009 12:38 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
session.CreateCriteria(GetType(Product)).SetCacheable(False) _
.CreateCriteria("Contact1")
.Add(Expression.Like("Name", "Piet")) _
.List(Of Product)()

or

session.CreateCriteria(GetType(Product), "p").SetCacheable(False) _
.CreateCriteria("p.Contact1", "c")
.Add(Expression.Like("c.Name", "Piet")) _
.List(Of Product)()

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Nhibernate CreateCriteria
PostPosted: Fri Sep 11, 2009 4:42 am 
Newbie

Joined: Thu Aug 13, 2009 3:52 pm
Posts: 4
Hi,

That works fine. Now I extended it a bit to 3 contact fields, not all fields are required.

If only one contact name matches I need it to be returned.

I tried:

Code:
session.CreateCriteria(GetType(Product), "p").SetCacheable(False) _
                .CreateCriteria("p.Contact1", "c1") _
                .CreateCriteria("p.Contact2", "c2") _
                .CreateCriteria("p.Contact3", "c3") _
                .Add(Expression.Disjunction() _
                             .Add(Expression.Like("c1.Name", keyword_like)) _
                             .Add(Expression.Like("c2.Name", keyword_like)) _
                             .Add(Expression.Like("c3.Name", keyword_like))) _
                .List(Of Product)()


But now all contacts must exists, so now I dont get any records back.


Top
 Profile  
 
 Post subject: Re: Nhibernate CreateCriteria
PostPosted: Fri Sep 11, 2009 4:46 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
CreateCriteria("p.Contact1", "c1", JoinType.LeftOuterJoin)

_________________
--Wolfgang


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

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.