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.  [ 12 posts ] 
Author Message
 Post subject: get the top 1 with nhibernate?
PostPosted: Fri Nov 28, 2008 9:11 am 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
i want to get the top 1 with nhibernate. i want to get all info in the table and by date and desc. im a newbie to nhibernate so i dont really know how o aproach this.

funny thing tho the most simplest things with sql is the hardest with nhibernate i think.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 10:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Both IQuery and ICriteria have a SetMaxResultSize() method. Setting this to 1 results in a TOP(1) query.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 11:07 am 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
im having a real hard time with this, this is how i do. what i want to happend is that i want to get the lunch with todays date. it is a single object no list.
the exception say that i cant cast NHibernate.Impl.CriteriaImpl to [Project Name][LunchDish.

public LunchDish GetTop1()
{
ICriteria crit = _session.CreateCriteria(typeof(LunchDish));
crit.Add(NHibernate.Criterion.Expression.Equals("LunchDate", DateTime.Now)).SetMaxResults(1);
LunchDish result = crit.List<LunchDish>()[0];
return result;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 11:22 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try LunchDish result = crit.UniqueResult<LunchDish>() and if there's only on lunch per day, you don't need SetMaxResult() either.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 11:37 am 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
im getting closer but still i cant compare the LunchDate with todays date. what should i use there?

public LunchDish GetTop1()
{
DateTime date = DateTime.Now;

ICriteria crit = _session.CreateCriteria(typeof(LunchDish));
crit.Add(NHibernate.Criterion.Expression.Equals("LunchDate", date));
LunchDish result = crit.UniqueResult<LunchDish>();
return result;
}

thx for your time


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 11:44 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post your mappings ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 11:48 am 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="[Project]" namespace="[Project]">
<class name="[Project]LunchDish" table="LunchDishTbl">
<id name="Id" column="id" type="Int32" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="Description" column="descr" not-null="false" />
<property name="LunchDate" column="lunch_date" not-null="false" />
<property name="KeyHole" column="keyhole" />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 12:07 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Mapping looks ok. What error do you get ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 2:24 pm 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
System.InvalidOperationException: Sequence contains no elements

there is a row in the table with that date, if its that the exception is?

i changed my code to this.

public LunchDish GetTop1()
{
DateTime date = DateTime.Now;

ICriteria crit = _session.CreateCriteria(typeof(LunchDish));
crit.Add(NHibernate.Criterion.Expression.Eq("LunchDate", date));
LunchDish result = crit.UniqueResult<LunchDish>();
return result;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2008 3:55 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Hmm strange ... sounds more like a basic problem that anything with the query in special.

Does the query work in this way:

ICriteria crit = _session.CreateCriteria(typeof(LunchDish));
IList<LunchDish> result = crit.List<LunchDish>();


Can you post the stacktrace ? Btw. which hibernate version do you use ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2008 4:12 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Does the exception only occur when you use SetMaxResults ? If so, then probably you also have to set the first result:

query.SetFirstResult(1);
query.SetMaxResults(1);

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2008 3:52 am 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
wolli wrote:
Hmm strange ... sounds more like a basic problem that anything with the query in special.

Does the query work in this way:

ICriteria crit = _session.CreateCriteria(typeof(LunchDish));
IList<LunchDish> result = crit.List<LunchDish>();


Can you post the stacktrace ? Btw. which hibernate version do you use ?


i can get all lunches in a list with no problem. i use the nhibernate 2.0 i think it is, the one that is avalible on for download from the site now. i use Jet driver to but that shouldent be any problems because everything else works. i would post the trace but its all in swedish. thing is i changed from 1 to 2 lunches, so i did the ilist and it works.

i tried the setfirst and setmax the other day but it didnt work, i cant remember what the exception was. but then i think the criteria query was wrong.

i am goin to do this top1 agin later today with another part of the application im building so i bet ill be back haha, so please check back on me.

thanks for your time, i really appricate it.


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