-->
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: Quering Entities
PostPosted: Mon Jul 13, 2009 8:18 pm 
Newbie

Joined: Sat Mar 21, 2009 5:22 am
Posts: 10
Hi,

I need some direction regarding the most appropriate way to query entities. Take the example below, I have TestEntityA and TestEntityB.
I want to retrieve all TestEntityA Where TestB.Code = 'test'

Would I have to create a Named Query to do this? Or can I utilise the ISession.CreateCriteria?

What is my best option to do this?
Thanks heaps.

public class TestEntityA
{
public TestEntityA() { }

public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual TestEntityB TestB { get; set; }
public virtual byte[] Version { get; set; }
}

public class TestEntityB
{
public TestEntityB() { }

public virtual Guid Id { get; set; }
public virtual string Code { get; set; }
public virtual string Description { get; set; }
public virtual byte[] Version { get; set; }
}


Top
 Profile  
 
 Post subject: Re: Quering Entities
PostPosted: Tue Jul 14, 2009 1:48 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can use any of these:

- criteria
- HBM
- named query

You normally use criterias if your query is dynamic (e.g. the user can specifiy which properties to search on). For everything else HBM is the easier way. And the best way to use HBM is to put it into a named query. Then you can change the query by simply manipulating the mapping file without touching the code (at least as long as you still get the same type of resultset).

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Quering Entities
PostPosted: Tue Jul 14, 2009 3:08 am 
Newbie

Joined: Sat Mar 21, 2009 5:22 am
Posts: 10
Thanks for the reply wolli.

How would the criteria query look like?

The following expression does not work
ICriterion cr1 = Expression.Sql("TestEntityB.Code = 'test'")

Thanks heaps.


Top
 Profile  
 
 Post subject: Re: Quering Entities
PostPosted: Tue Jul 14, 2009 3:33 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Depends on the mapping. If TestEntityB is an association, something like this:

Code:
session.CreateCriteria(typeof(TestEntityA),"a")
      .CreateCriteria("a.TestEntityB", "b")
      .Add(Expression.Eq("b.Code", "test")


You should have a look at the docs:

http://nhforge.org/doc/nh/en/index.html#querycriteria

_________________
--Wolfgang


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.