-->
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.  [ 7 posts ] 
Author Message
 Post subject: Get a list of a class by one of it's field?
PostPosted: Sun Jul 23, 2006 2:04 am 
Newbie

Joined: Tue Jul 18, 2006 9:06 am
Posts: 15
Location: .NET
Hi!

I am a new user of NHibernate. From the beginning I'd like understand the right principles of working with NHibernate.
So, maybe my question is a simple one but I have doubts...

Let's say I have a Product class and it has Category field.
How can I receive all products by a category?

Code:
public List<Product> GetByCategory(int categoryId)
{

}


Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 4:22 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Try:

Code:
Query q = session.createQuery("from Product p where p.category.categoryid= :categoryId");
q.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 1:28 am 
Newbie

Joined: Tue Jul 18, 2006 9:06 am
Posts: 15
Location: .NET
anandbn wrote:
Try:
Code:
Query q = session.createQuery("from Product p where p.category.categoryid= :categoryId");
q.

Thank you.
So, I must use HQL to get, for example, Products by a Category? Can't I do it with mapping in xml file and...?

_________________
"A successful person is one who can lay a firm foundation with the bricks that others throw at him."
http://www.russiangeek.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 2:17 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
You could use Filters(if I remember correct). You can enable and disbale them programmatically. Refer to Chapter 17 of the reference guide.

Additionally you can define the HQL query in the mapping XML file and use Session.createNamedQuery("name in mapping xml");. That way the HQL can be modified without modifiying Java code.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 2:27 am 
Newbie

Joined: Tue Jul 18, 2006 9:06 am
Posts: 15
Location: .NET
anandbn wrote:
You could use Filters(if I remember correct). You can enable and disbale them programmatically. Refer to Chapter 17 of the reference guide.

Additionally you can define the HQL query in the mapping XML file and use Session.createNamedQuery("name in mapping xml");. That way the HQL can be modified without modifiying Java code.

Thanks, I'll try...

_________________
"A successful person is one who can lay a firm foundation with the bricks that others throw at him."
http://www.russiangeek.com


Top
 Profile  
 
 Post subject: Re: Get a list of a class by one of it's field?
PostPosted: Tue Jul 25, 2006 3:39 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
igoro1975 wrote:
Let's say I have a Product class and it has Category field.
How can I receive all products by a category?

Code:
public List<Product> GetByCategory(int categoryId)
{

}


It is assumed that you have open session object.

Code:
return session.CreateCriteria(typeof(YourType))
  .Add(Expression.Eq("CategoryID", categoryId))
  .List();

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject: Re: Get a list of a class by one of it's field?
PostPosted: Tue Jul 25, 2006 4:09 am 
Newbie

Joined: Tue Jul 18, 2006 9:06 am
Posts: 15
Location: .NET
rolandz wrote:
igoro1975 wrote:
Let's say I have a Product class and it has Category field.
How can I receive all products by a category?

Code:
public List<Product> GetByCategory(int categoryId)
{

}


It is assumed that you have open session object.

Code:
return session.CreateCriteria(typeof(YourType))
  .Add(Expression.Eq("CategoryID", categoryId))
  .List();

O, it looks more readable :)
Thank you.

_________________
"A successful person is one who can lay a firm foundation with the bricks that others throw at him."
http://www.russiangeek.com


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