-->
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: Query using associated object properties?
PostPosted: Wed Sep 20, 2006 12:59 pm 
Newbie

Joined: Wed Sep 20, 2006 12:33 pm
Posts: 2
Hi, I'm having some trouble querying using properties of associated objects.

To boil down to an easy example, let's say I've got a BlogPost class, and a User class. Each BlogPost belongs to a User.

BlogPost is mapped to User using a many-to-one relationship. However, I can't seem to retrieve ILists of BlogPosts belonging to a given User's username property.

I'd like to grab the list like this:

Code:
IList blogPosts = ListByPropertyValueOrderBy(Type.GetType(BlogPost.GetType().ToString(), "User.Username", (object)"sammy", "CreatedOn", "DESC");


Is it in fact considered legal to do that "User.UserName" in a situation like this?

Here's the retrieval method:

Code:
public IList ListByPropertyValueOrderBy(Type type, string propertyName, object propertyValue, string orderPropertyName, string orderValue)
{
   try
   {
      ICriteria crit=m_session.CreateCriteria(type);
      crit.Add(Expression.Eq(propertyName, propertyValue));
      if(orderValue == "ASC")   
      {
         crit.AddOrder(Order.Asc(orderPropertyName));
      }
      else if(orderValue == "DESC")
      {
         crit.AddOrder(Order.Desc(orderPropertyName));
      }
      return crit.List();
   }
   catch (Exception ex)
   {            
      throw ex;
   }
}



I've also tried it using HQL. This works:

Code:
IList blogPosts = m_session.CreateQuery( "from BlogPost as blogPost where blogPost.User.UserName = 'sammy'")


Any idea whether it's possible using the Criteria interface?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 3:46 pm 
Newbie

Joined: Fri Sep 22, 2006 3:24 pm
Posts: 2
Have you tried "this.User.Username" instead?

Otherwise, you could probably insert .createAlias("User", "u") and then pass in "u.Username" instead of "User.Username".

Also, shouldn't your if statements be stated if (order.equals("ASC"))? I don't think you're going to order anything otherwise.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 23, 2006 12:48 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Quote:
Also, shouldn't your if statements be stated if (order.equals("ASC"))? I don't think you're going to order anything otherwise.

No, == works like Equals for strings in C#.


Top
 Profile  
 
 Post subject: interesting idea with aliasing
PostPosted: Sat Sep 23, 2006 8:40 am 
Newbie

Joined: Wed Sep 20, 2006 12:33 pm
Posts: 2
Hi,

Interesting idea with using an alias, I'll try it out but may not get it working until Monday (my "Hibernate in Action" is at work currently).

Will report back if it looks like it'll work, thanks for the reply.

In response to the question about "==", it works fine. I've actually written up a couple of classes that deal with almost all of the data requests for any type in a web app, so basically I can do up a pseudo-Ruby on Rails style query using this sort of syntax (let's say Article is a persistent page in a web site):


// get article with id of 5
Article article = new Article().Get(5);

or

// get article where the title is "Foo"
Article article = new Article().Get("Title", (object)"foo");

or

// list all active articles
IList articles = new Article().List("Active", (object)true);

The chunk of code I quoted in the first post is part of this class; I'm really enjoying web apps a lot more since integrating it into the stuff we're doing, it makes data work so much easier.

Would be nice if I could figure out how to make the retrieval calls into static methods, like:

Article article = Article.Get(5);

But currently I can't get this to work...


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.