-->
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: Prevent NHibernate to query the DB when Id is -1
PostPosted: Thu Jan 28, 2010 4:32 am 
Newbie

Joined: Thu Jan 28, 2010 4:16 am
Posts: 1
Hi NHibernaters

I have the following db tabel
Code:
------------------------
| id | parentId | name  |
| 1  | 2        | john  |
| 2  | -1       | james |
...
-------------------------


And the following mapping
Code:
<class name="customer" table="customer">
  <property name="name" column="name" />
  <many-to-one name="parent" column="parentId" not-found="ignore" />
</class>


And following class
Code:
public class Customer
{
public string name { get; set; }
public Customer parent { get; set;}
}


When getting a customer, I notice in a profiler that there are queries to the db like the following

Code:
Select * from Customer Where id = -1


This obviously results in zero rows, cause there will never be a customer with id -1.
How can I tell NHibernate that such a query is useless, and the it can return zero, without querying the DB.

Need more info, please don't hesitate to ask.
Thanks for your time and effort.

EDIT: I noticed that this topic should be in the .Net section, however it is not .Net specific. Sorry

Regards
Wim


Top
 Profile  
 
 Post subject: Re: Prevent NHibernate to query the DB when Id is -1
PostPosted: Thu Jan 28, 2010 6:03 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Use "null" instead of some special value (-1). Using not-found="ignore" is a way to treat special values as null, but Hibernate can't know that a value is special until the database has been queried. It may be possible to overcome this by implementing a custom user type. I have not tried it myself, but I found an example that is similar to your problem. See example 3: http://i-proving.ca/space/Technologies/ ... +Hibernate


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.