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: Checking if record exists
PostPosted: Wed Aug 16, 2006 3:08 am 
Newbie

Joined: Wed Aug 16, 2006 2:49 am
Posts: 14
What's the common/best approach for determining if a certain record already exists? Given the table has a primary key. I'd like to check first if the data is not existing before I insert one.

I'm new to nhibernate. I've just done a simple mapping and data retrieval with this using c#. Normally w/o nhibernate, I'll create a stored procedure like doesUserExist. But with nhibernate, what approach is done?

Thanks.


Top
 Profile  
 
 Post subject: Re: Checking if record exists
PostPosted: Wed Aug 16, 2006 8:51 am 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
kokoness wrote:
What's the common/best approach for determining if a certain record already exists? Given the table has a primary key. I'd like to check first if the data is not existing before I insert one.

I'm new to nhibernate. I've just done a simple mapping and data retrieval with this using c#. Normally w/o nhibernate, I'll create a stored procedure like doesUserExist. But with nhibernate, what approach is done?

Thanks.


You would do something similar to what you've done in the past, except that instead of using a stored procecure, you would use an NHiberanate query. There is the option of using an ICriteria query, or you can develop an HQL query.

If you are using the Primary Key as your search criteria you can issue a Session.Load(...) statement. If the object doesn't exist I would expect it to return null.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 9:20 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
session.Get will return null, Load won't (it will return a proxy or throw an exception).


Top
 Profile  
 
 Post subject: re: Checking if record exists
PostPosted: Wed Aug 16, 2006 9:35 pm 
Newbie

Joined: Wed Aug 16, 2006 2:49 am
Posts: 14
gcook1:
I'll look into HQL query. Thanks a lot!


sergey:
Thanks for the additional info!


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.