-->
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.  [ 9 posts ] 
Author Message
 Post subject: Entity exist? Based on unique constraint
PostPosted: Tue Oct 16, 2007 7:17 pm 
Newbie

Joined: Tue Oct 16, 2007 7:10 pm
Posts: 3
Hi,

I am new to NHibernate.
Can somone give me a hint how I find out if an entity with a unique constraint already exist in database?

Thx.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 8:28 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
There's a number of ways to do this. I've been using this lately:

Code:
int matchCount = session.CreateQuery(scalarQuery).UniqueResult();


the scalar query is an HQL query using select count(*). If the where clause specifies a unique column value then one or zero should return. You can also run aggregate queries using straight SQL if you wanted...you would then call session.CreateSQLQuery().

You could also use a get while specifying the unique field as a criterium. You should get a null if it is not already in the database.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 8:45 pm 
Newbie

Joined: Tue Oct 16, 2007 7:10 pm
Posts: 3
Thank you for the reply.

I am trying this:

int test = (int)session.CreateQuery("SELECT count(*) from User u WHERE u.UserName = :uname").SetString("uname", nickName).UniqueResult();

I get a cast exception. Looks like the query is performes correct. Can't the Object from UniqueResult be cast to an int?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 9:32 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
If if the object reference is type int, sure. That's what I've done, well not using a direct cast. I wrapped the ExecuteScalarQuery in a method like so:
Code:
public object ExecuteScalarQuery(string query)
{
   return NHibernateSession.CreateQuery(query).UniqueResult()
}


I called it from VB.Net using CType(dao.ExecuteScalarQuery(query), INTEGER
Code:
...
return CType(dao.ExecuteScalarQuery(query), Integer)


I also wasn't using SetString(), but I'd imagine it would just return the modified IQuery instance. Not sure, I'd probably break it down into a few lines to ensure there were no issues. I'd also ensure that all input params are valid (like uname), check the SQL generated by NHibernate (in log file), run the query independently (which should make a difference here), then I'd ensure that I'm not getting a null back from the query (which I'm not sure why that would happen off hand).

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 7:59 am 
Newbie

Joined: Tue Oct 16, 2007 7:10 pm
Posts: 3
Following works. Thanks for your help!

Object test = session.CreateQuery("SELECT count(*) from User u WHERE u.UserName = :uname").SetString("uname", nickName).UniqueResult();
log.Debug("****Count obj = " + test.ToString());
int myint = Convert.ToInt32(test);
log.Debug("****Count int = " + myint.ToString());


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 10:34 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
I believe COUNT returns a long in NHibernate as of version 1.2.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 1:36 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
merge_s.rottem wrote:
I believe COUNT returns a long in NHibernate as of version 1.2.

Symon.


Hmm, I don't know for sure. I know that implementers of IExpectation use in for rowcounts. I would have guessed that it depended on what the database returns. In the tests it also looked as if NHibernateUtil.Int32 was used as the type for count when projections were defined (seen in CriteriaQueryTest).

I could see a long getting returned, although I'd probably never want to use too many aggregate funtions on a table with the over 2 billion records needed to justify a long...I joke.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 3:03 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
I'm pretty sure: http://www.hibernate.org/Documentation/ ... nGuide#A22

:)

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 8:43 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Nice! I didn't know that. I'll definitely keep that in mind (and change my code for good measure).

...I still wouldn't want to need that size...
;)

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


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