-->
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.  [ 6 posts ] 
Author Message
 Post subject: many-to-one select
PostPosted: Mon Nov 20, 2006 12:24 pm 
Newbie

Joined: Tue Sep 26, 2006 10:59 am
Posts: 15
Hello.

I have two tables: Requests and Users.

Table Request:
- Id
- Text
- UserId

Table User
- Id
- Name


So the map-file of Request contains the string
Code:
<many-to-one name="User" column="UserId">


Now I need to get all Requests where Usert = null or User.Name = ''

How can I do this by using the Criteria?

If I write this:
Code:
ICriteria c = session.CreateCriteria.(typeof(Request));
c.Add(Expression.Or(Expression.IsNull("User"), Expression.Eq("User.Name", "")));
IList il = c.List();

I send so exception: could not resolve property: User.Name of: Request

If I add the Alias:
Code:
ICriteria c = session.CreateCriteria.(typeof(Request));
c.CreateAlias("User", "user_");
c.Add(Expression.Or(Expression.IsNull("User"), Expression.Eq("user_.Name", "")));
IList il = c.List();

This code generate INNER JOIN with Users table, and Request with null-user is absent in selection.

Please, help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 4:45 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
You need to create another criteria in the chain:

session.CreateCriteria.(typeof(Request)).CreateCriteria("User").Add(Expression.Eq(...)))


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 21, 2006 4:28 am 
Newbie

Joined: Tue Sep 26, 2006 10:59 am
Posts: 15
Thank you for your answer, but it is not the solution.
The code
Code:
CreateCriteria("User")
like
Code:
CreateAliace
geterate INNER JOIN with User table, so Requests with NULL user have not a place in selection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 21, 2006 4:41 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
There is a CreateCriteria/CreateAlias overload which takes JoinType as a parameter, use that. I'm not sure if it's present in Beta2, it was added recently.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 21, 2006 5:55 am 
Newbie

Joined: Tue Sep 26, 2006 10:59 am
Posts: 15
Thanks for good news! It's a pity that it isn't present Beta 2. We'll expect Beta 3 ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 21, 2006 10:14 am 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
For now, maybe you can use a union in HQL?


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