-->
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: How should I implement [try to get and create if not found]
PostPosted: Thu Jan 18, 2007 8:42 am 
Newbie

Joined: Thu Jan 04, 2007 3:56 pm
Posts: 11
Hi,

I'm using a table that gets most of it's data including the primary key from an external system.

The code that I expected to work looks something like this.

Code:
try
{
   // try to get the object from database...
   return session.Get<MyClass>(externalId);
}
catch(ObjectNotFoundException)
{
   // if not there create a new one...
   return new MyClass(externalId);
}


I think the Get<T> method used to throw right away when calling it with an erraneous id, but instead it returns an invalid object. This objects throws when I try to get any properties. Can I change anything to get the exception when calling Get<T>? Any suggestions on an alternative strategy for this situation?

Thanks, Cristian

nhibernate version: 1.2.0.2002

mapping: A subclassed class with assigned id:s


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 9:12 am 
Newbie

Joined: Wed Jan 17, 2007 7:29 am
Posts: 10
You can use the Load<T> method. It will throw an exception. Or you can test if the object returned by Get<T> is null.


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.