-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query By Example
PostPosted: Thu Feb 09, 2006 11:56 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi. I'm using Query By Example and I have two problems:

1) The SQL generated by ICriteria.List() is using the DateTime fields. I've used ExcludeNulls() and ExcludeZeroes(), but the DateTime fields still there.

The code:

Code:
private IList ListByExample(object sample)
{
   ISession session = _factory.OpenSession();
   ICriteria criteria = session.CreateCriteria(sample.GetType());

   Example ex = Example.Create(sample);
   ex.ExcludeNulls();
   ex.ExcludeZeroes();
   criteria.Add(ex);               

   return criteria.List();
}


The SQL generated:

Code:
SELECT ... FROM ... WHERE (this.CLI_DATAMAIORFATURA = '0001-01-01 00:00:00' and this.ATI_ID = '3' and this.CLI_DATAPRIMEIRACOMPRA = '0001-01-01 00:00:00' and ...)


2) With the same code above, NHibernate generates the SQL using just the properties or just the Composite-Id (or a simple Id) as example.

How can I do to get a list of objects by example using the Id and Properties as example at the same query?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 12:33 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hum, I think I've found the answer:

- http://forum.hibernate.org/viewtopic.php?t=927063

And

- http://forum.hibernate.org/viewtopic.php?t=938036

Quote:
...QBE ignores ID fields. For simple primary keys it makes sense, but composite IDs create surprises....


So, does NHibernate already implement the QBE using id/composite-id?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 4:24 am 
Newbie

Joined: Tue Apr 10, 2007 10:24 am
Posts: 2
Hi!
I have the same problem - I can't force NHibernate to omit DateTime properties having default values (01-01-01) in query by example search. Do you found solution?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 4:31 am 
Newbie

Joined: Thu Apr 03, 2008 4:20 am
Posts: 1
Hi,
I resolved this problem adding a trick at the code that search for DateTime fields inside the object and if valued 1/1/1 exclude it.

This is the piece of code that I wrote:

Code:
private IList ListByExample(object sample)
{
   ISession session = _factory.OpenSession();
   ICriteria criteria = session.CreateCriteria(sample.GetType());

   Example ex = Example.Create(sample);
   ex.ExcludeNulls();
   ex.ExcludeZeroes();

   foreach (PropertyInfo info in (sample.GetType()).GetProperties())
   {
         if (info.PropertyType == typeof(DateTime))
         {
                DateTime dt = (DateTime) info.GetValue(sample, info.GetIndexParameters());
                if (dt <= DateTime.MinValue)
                    e.ExcludeProperty(info.Name);
         }
   }

   criteria.Add(ex);               

   return criteria.List();
}


Top
 Profile  
 
 Post subject: Re: Query By Example
PostPosted: Sat Oct 16, 2010 2:19 pm 
Newbie

Joined: Sat Oct 16, 2010 2:16 pm
Posts: 1
You can get all your rows back if you use a Nullable Datetime as property

public virtual Nullable<DateTime> StartDate { get; set; }

sincerely
Mehmet Rasim Inceoglu


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