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: ICriteria for a join table
PostPosted: Fri Feb 13, 2009 1:58 pm 
Newbie

Joined: Fri Feb 13, 2009 12:11 pm
Posts: 2
Hi,

Just a quick question. If I've got 2 tables that are joined in a 3rd table with a many-to-many relationship, is it possible to write an ICriteria with expressions in one of the tables and the join table?

Lets say the mapping file looks something like:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping...
.
.
.
    <bag name ="Bag" table="JoinTable" cascade ="none">
      <key column="Data_ID"/>
      <many-to-many class="Data2" column="Data2_ID"/>
    </bag>
.
.
.
</hibernate-mapping>


Is it then possible to write an ICriteria like the following?

Code:
            ICriteria crit = session.CreateCriteria(typeof(Data));
            crit.Add(Expression.Eq("Name", name));
            crit.Add(Expression.Between("Date", startDate, endDate));
            crit.Add(Expression.Eq("Bag", data2IDNumber));


When I try this, it tells me I the expected type is IList<Bag>, whereas the actual type is Bag.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 2:07 pm 
Newbie

Joined: Fri Feb 13, 2009 1:56 pm
Posts: 4
Hi,

yes you can. Do something like this

ICriteria crit = session.CreateCriteria(typeof(TABLE1));
crit.Add(Expression.Eq("Name", name));
crit.Add(Expression.Eq("TABLE2.Bag", data2IDNumber));

where Name belongs to Table1 and Bag is the Table2 attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 2:57 pm 
Newbie

Joined: Fri Feb 13, 2009 12:11 pm
Posts: 2
DashaRV wrote:
Hi,

yes you can. Do something like this

ICriteria crit = session.CreateCriteria(typeof(TABLE1));
crit.Add(Expression.Eq("Name", name));
crit.Add(Expression.Eq("TABLE2.Bag", data2IDNumber));

where Name belongs to Table1 and Bag is the Table2 attribute.


Hi, thanks for the help. I'm not fully understanding what you're saying.

For examples sake, lets call the tables classes: Table1, Table2, and TableJN. TableJN joins Table1_ID and Table2_ID.

Mapping:
Code:
    <bag name ="Items" table="TableJN" cascade ="none">
      <key column="Table1_ID"/>
      <many-to-many class="Item" column="Table2_ID"/>
    </bag>


You're saying my ICreateria would have to be setup like this?
Code:
     ICriteria crit = session.CreateCriteria(typeof(Table1)
     crit.Add(Expression.Eq("Name", name));
     crit.Add(Expression.Eq("Table2.Item", Table2_ID));


Then it just says it couldn't resolve the property.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 5:50 am 
Newbie

Joined: Fri Feb 13, 2009 1:56 pm
Posts: 4
You have 2 options in this case. Try to change your mappings or use detached criteria. I'm using mappings like this:

<class name="ExchangeRate" table="ExchangeRates">
<id name="Id" type="System.Guid" column="Id">
<generator class="guid" />
</id>
</property>
<property name="Rate" type="Decimal">
<column name="Rate" sql-type="decimal(19,5)" not-null="true" unique="false"/>
</property>
<many-to-one name="Currency" class="Currency" column="CurrencyId" not-null="true" cascade="none" lazy="false" fetch="join" />
</class>

with this mappings you can use criteria like I told you. If you keepyour mappings as is, use detached criteria.

use this sample from NHibernate help:

DetachedCriteria avgWeightForSex = DetachedCriteria.For(typeof(Cat), "cat2")
.SetProjection( Projections.Avg("Weight") )
.Add( Expression.EqProperty("cat2.Sex", "cat.Sex") );
session.CreateCriteria(typeof(Cat), "cat")
.Add( Subqueries.Gt("weight", avgWeightForSex) )
.List();


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.