-->
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.  [ 9 posts ] 
Author Message
 Post subject: implicit join... is it available in NHibernate
PostPosted: Sun May 14, 2006 9:46 am 
Regular
Regular

Joined: Sun Feb 12, 2006 10:18 pm
Posts: 60
Location: Rosario, Argentina
that's pretty much my question...

I need to do a query like this "Person.Address.Street like 'something%'"
The Hibernate reference says that is an implicit join, so writting the code like that should work. NHibernate reference says nothing about HQL at all.
When I try to reach the Street property of the class Adress, it says it doesn't exist (if I see the properties collection of Address, it only has "Id")

So, are implicit joins available in NHibernate, and I'mm just doing something wrong, or are they not available yet?

Alejandro.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 12:55 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Hi!

Quote:
I need to do a query like this "Person.Address.Street like 'something%'"


You are right, that should work

Quote:
NHibernate reference says nothing about HQL at all.


You mean there is no documentation for HQL? That is not true: http://www.hibernate.org/hib_docs/nhibe ... ryhql.html

Quote:
When I try to reach the Street property of the class Adress, it says it doesn't exist


How does it "says it doesn't exist"... it throws an exception? what is the stacktrace? what is inside your mapping file? how you classes look like?

Quote:
if I see the properties collection of Address, it only has "Id"

How do you see the properties collection of Address?

Quote:
So, are implicit joins available in NHibernate, and I'mm just doing something wrong, or are they not available yet?

I guess you are doing something wrong... and I think you need to post more information


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 5:37 pm 
Regular
Regular

Joined: Sun Feb 12, 2006 10:18 pm
Posts: 60
Location: Rosario, Argentina
I checked the link you sent me, and it doesn't say anything about implicit joins but it does give an example where it's clear that you can...


Code:
from Eg.Foo foo
where foo.Bar.Baz.Customer.Address.City is not null


I'll check what I'm doing wrong and try to fix it.

To answer your questions, I debugged the error it threw and checked the values of the collections NHibernate uses.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 10:21 am 
Regular
Regular

Joined: Sun Feb 12, 2006 10:18 pm
Posts: 60
Location: Rosario, Argentina
I can't see what's wrong...

I've got this: a Localidad (City) has a property Provincia (State), and a Provincia has a collection of Localidad. the mappings are as follows:

Provincia (State)
Code:
    <class name="ReglasPrestamos.Provincia, ReglasPrestamos" table="Provincia">
        <id name="Id" type="Int32" unsaved-value="0">
            <column name="id" />
            <generator class="native" />
        </id>
        <many-to-one name="Pais" class="ReglasPrestamos.Pais, ReglasPrestamos" fetch="select">
            <column name="id_pais" not-null="true" />
        </many-to-one>
        <property name="Nombre" type="String">
            <column name="nombre" not-null="true" />
        </property>
        <set name="Localidades" inverse="true">
            <key>
                <column name="id_provincia" not-null="true" />
            </key>
            <one-to-many class="ReglasPrestamos.Localidad, ReglasPrestamos" />
        </set>
    </class>


Localidad (City)

Code:
    <class name="ReglasPrestamos.Localidad, ReglasPrestamos" table="Localidad">
        <id name="Id" type="Int32" unsaved-value="0">
            <column name="id" />
            <generator class="native" />
        </id>
        <many-to-one name="Provincia" class="ReglasPrestamos.Provincia, ReglasPrestamos" fetch="select">
            <column name="id_provincia" not-null="true" />
        </many-to-one>
        <property name="Nombre" type="String">
            <column name="nombre" not-null="true" />
        </property>
    </class>



if I make a Criteria Query

Code:
ICriteria auxCriteria = session.CreateCriteria(typeof(T)) //Generics
auxCriteria.Add(NHibernate.Expression.Expression.Like("Nombre", //City's name
"%ros%"));
auxCriteria.Add(NHibernate.Expression.Expression.Like("Provincia.Nombre", //State.Name
"%sa%"));


when it tryes to add "Provincia.Nombre" it throws the exception

Quote:
could not resolve property:Provincia.Nombre of :Localidad


this exception is thrown by the class AbstractPropertyMapping and method string[] ToColumns( string alias, string propertyName )

so... can anyone find the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 12:41 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Quote:
Code:
ICriteria auxCriteria = session.CreateCriteria(typeof(T)) //Generics
auxCriteria.Add(NHibernate.Expression.Expression.Like("Nombre", //City's name
"%ros%"));
auxCriteria.Add(NHibernate.Expression.Expression.Like("Provincia.Nombre", //State.Name
"%sa%"));



First... why use generics in this case? it seems to me that is a "for one type only" kind of critery query... (and the problem could be on the definition of the concrete type for T, maybe you are sending "State" instead of "City")

Second... I havent tried implicit joins in criteria queries (I am not saying that they are not possible, I just haven't tried them... have you tried this same query with HQL?)


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 1:10 pm 
Regular
Regular

Joined: Sun Feb 12, 2006 10:18 pm
Posts: 60
Location: Rosario, Argentina
I use generics because I have a class that handles all the DAO, and thanks to generics it can handle all my persistent classes. For simplification, I replaced some values in the code I pasted, so it looks "for one type only", but it's not...
I use Criteria in stead of HQL because my queries are generated at run time, based on filters selected by the user

As tenwit said in a previous post of mine:
Quote:
- CreateCriteria uses Criteria, which is most useful if your query is to be generated at run time.
- CreateQuery uses HQL, which is most useful if your query doesn't change (only its parameters do).


so, I can't use plain HQL...


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 6:10 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Quote:
so, I can't use plain HQL...


It is more like you shouldn't... and you example seems IMHO to "only for City" to make this a concern...

Bur ff you have to use Criteria queries, then I guess the way to go is this: http://www.hibernate.org/hib_docs/nhibe ... sociations


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 6:30 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
yes, following on luxspes statements, your query should look like:

Code:
return session.CreateCriteria(typeof(Localidad))
    .Add(Expression.Like("Nombre","%ros%"));
    .CreateCriteria(typeof(Provincia))
        .Add(Expression.Like("Nombre","%sa%"));
    .List();


Last edited by devonl on Tue May 16, 2006 2:43 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 2:04 pm 
Regular
Regular

Joined: Sun Feb 12, 2006 10:18 pm
Posts: 60
Location: Rosario, Argentina
Thank's luxspes for that link, I understood how to do this and I modified my generic class to be able to understand this new "createCritera" for inner criteria... so far it's working, though I haven't tested it too much.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.