-->
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: Reusing Criteria for RowCount first and then the actual Rows
PostPosted: Wed May 02, 2007 1:13 pm 
Newbie

Joined: Fri Sep 29, 2006 6:52 am
Posts: 17
Hi!

I'm not sure if this is a bug in NHibernate or if the bug sits in front of the monitor.

I have two classes in a many-to-one-relationship:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
  <class name="Domain.Foo, Domain">
    <id name="Id" access="nosetter.lowercase-underscore">
      <generator class="native" />
    </id>
    <many-to-one name="Bar" lazy="false"/>
  </class>
</hibernate-mapping>


Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
  <class name="Domain.Bar, Domain">
    <id name="Id" access="nosetter.lowercase-underscore">
      <generator class="native" />
    </id>
  </class>
</hibernate-mapping>


Code:
   public class Foo
    {
        private long _id = 0;
        private Bar _bar;

        public long Id
        {
            get { return _id; }
        }

        public Bar Bar
        {
            get { return _bar; }
            set { _bar = value; }
        }
    }

    public class Bar
    {
        private long _id = 0;

        public long Id
        {
            get { return _id; }
        }
    }


Now I use the same criteria to read the row count and the first ten rows (needed for a Paginator class that takes a Criteria as filter condition):

Code:
        [Test]
        public void TestCriteria()
        {
            Bar bar = new Bar();
            _session.Save(bar);

            Foo foo = new Foo();
            foo.Bar = bar;
            _session.Save(foo);

            ICriteria criteria = _session.CreateCriteria(typeof(Foo));

            // row Count
            criteria.SetFirstResult(0);
            criteria.SetMaxResults(NHibernate.Engine.RowSelection.NoValue);
            criteria.SetProjection(NHibernate.Expression.Projections.RowCount());
            Assert.AreEqual(1, criteria.UniqueResult<int>());
            criteria.SetProjection(null);

             // rows
            criteria.SetFirstResult(0);
            criteria.SetMaxResults(10);
            IList<Foo> list = criteria.List<Foo>();
            Assert.IsNotNull(list);
            Assert.AreEqual(1, list.Count);
        }


But Nhibernate raise an NHibernate.ADOException with the Message "Unable to perform find" and an inner exception of type System.ArgumentException with the message (translated from German) "System.Object[] is not a value of Type Domain.Foo and can not be used in this generic collection".

If I read the rows before the row count, it works fine. If I make the Id property of Bar virtual and set default-lazy="true" in Bar.hbm.xml it works fine too.

Any ideas what's going wrong here? I've tested this with 1.2.0-CR2 and the current SVN snapshot of 2.0.0.

Thanks,

Tobias


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 5:17 am 
Newbie

Joined: Fri Sep 29, 2006 6:52 am
Posts: 17
I digged a little bit deeper into the NHibernate sources and figured out, that SetProjection(null) does not work. I would expect this to reset the Projection on a Criteria, but this doesn't work and there seems no other way to do this.

I've opened new Jira issue: http://jira.nhibernate.org/browse/NH-994

A small patch is attached, to fix this problem.

Tobias


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.