-->
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.  [ 2 posts ] 
Author Message
 Post subject: Fetching objects
PostPosted: Thu Apr 19, 2007 3:15 pm 
Newbie

Joined: Thu Apr 19, 2007 12:51 pm
Posts: 3
Is it possible to do something like this:

Code:
  User usr = new User();
  usr.FirstName = "J%";
  usr.LastName = "Gore";
  IList<User> users = session.Find(usr) as IList<User>;


If not, how do I retrieve the table <=> object mappings (to be able to to the bindings myself)?[/code]

Edit 1:

Ok. I've found the CreateCriteria command =)
I know that the method is very limited. But here is my quick and dirty implementation.

Code:
    public class Manager
    {
        public IList<T> GeneralFind<T>(T searchPattern)
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            ICriteria criteria = session.CreateCriteria(typeof(T));

            // Reflect all properties to get search params
            PropertyInfo[] properties = searchPattern.GetType().GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object value = property.GetValue(searchPattern, null);

                // Strings can use Like
                if (value.GetType() == typeof(string))
                {
                    string valueStr = value as string;
                    if (valueStr.IndexOf("%") >= 0)
                        criteria.Add(Expression.Like(property.Name, value));
                    else
                        criteria.Add(Expression.Eq(property.Name, value));
                }
                else
                    criteria.Add(Expression.Eq(property.Name, value));
            }

            return criteria.List<T>();
        }
    }

  //And to fetch data:
  User usr = new User();
  usr.FirstName = "J%";
  usr.LastName = "Gore";

  Manager mgr = new Manager();
  IList<User> users = mgr.GeneralFind<User>(usr);


I made it general by using reflection.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 19, 2007 8:43 pm 
Newbie

Joined: Sat Apr 07, 2007 8:02 pm
Posts: 4
Save yourself the reflection and use the NHibernate Metadata api.

http://www.hibernate.org/hib_docs/nhibe ... a-metadata

Also, it looks like you're doing a query by example. See the Example object which supports QBE.

Code:
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
Example example = Example.Create(exampleInstance);


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