tags:

views:

88

answers:

1

Hey there,

I'm using NHibernate. I have two tables: Owner which has

<id name="SpecificNumber" column="SpecificNumber" type="int">
   <generator class="assigned"/>
</id>
<set name="SubTable" table="SubTable">
    <key column="Owner_id"/>
    <one-to-many class="SubTable"/>
</set>

and a table SubTable

which has

<many-to-one name="Owner" class="Owner" column="Owner_id"/>

Now my C# class Owner doesn't have a Owner_id property, and my SubTable class doesn't have one either. My SubTable class does have a property of Type Owner though.

In the database everything seems to be correct. However when I try to retrieve certain Subtable-rows which match a certain Owner-Id by using this statement

ICriteria criteria = session.CreateCriteria<SubTable>();
criteria.Add(Expression.Eq("Owner_id",3));

I get this exception:

"could not resolve property: Owner_id of: SubTable"

So I guess hibernate would like the SubTable class to also have a Owner_id property. Is there a way to do this without doing so? My SubTable class does a property which holds a reference to a Owner object itself. Shouldn't that be sufficient? Or what is the best way to this kind of join select in hibernate without messing up the model too much?

What I'm trying to achieve is that the SpecificNumber of Owner is used as a reference in SubTable. And I want to query the SubTable for rows having a certain Owner.

Thanks!

+2  A: 

In your criteria query, you should not refer to columns in your database (as I can see, Owner_id is a column in a table), but you should use the properties on your objects. The ICriteria API and HQL queries work on your objects (and their properties), so, you should not refer to db-columns.

ICriteria crit = session.CreateCriteria <SubTable>();
crit.AddAlias ("Owner", "o");
crit.Add (Expression.Eq("o.SpecificNumber", 23));
Frederik Gheysels
Thanks dude! You helped me alot.
noisecoder