-->
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: NHibernate.Linq last release and lazy loading.......
PostPosted: Mon Jul 27, 2009 2:36 am 
Newbie

Joined: Wed Jul 22, 2009 8:40 am
Posts: 5
Hello everybody! I try to use NHibernate.Linq to query from database and catch NullReferenceExceprion:
Code:
        [Test]
        public void Should_Get_By_Dict_Value()
        {           
            using (ISession s = NHibernateHelper.GetCurrentSession())
            {
                var queryable = s.Linq<A>();
                var first = queryable.First(a => a.Items["first"].Name == "first b");
                Assert.That(first, Is.Not.Null);
            }
        }

But this work correctly:
Code:
       [Test]
        public void Should_Get_By_Dict_Value_Work()
        {       
            using (ISession s = NHibernateHelper.GetCurrentSession())
            {
                var queryable = s.Linq<A>();
/*---->*/  var list = queryable.ToList();  /*<----*/
                var first = list.First(a => a.Items["first"].Name == "first b");
                Assert.That(first, Is.Not.Null);
            }
        }

I think this is the bag or NHibernate.Linq don't support lazy loading......
A class implementation, mapping and test:
Code:
public class A
    {
        private int? _id;
        private string _name;
        private IDictionary<string, B> _items;

        public A() { }

        public int? Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public IDictionary<string, B> Items
        {
            get { return _items; }
            set { _items = value; }
        }
    }

    public class B
     {
         private int? _id;
         private string _name;
   
         public B() { }
   
         public int? Id
         {
            get { return _id; }
            set { _id = value; }
         }
   
         public string Name
         {
            get { return _name; }
            set { _name = value; }
         }
      }

Mapping file
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHTest" namespace="NHTest" default-lazy="false">
   <class name="A" table="a">
      <id name="Id" column="id" unsaved-value="null">
         <generator class="native" />
      </id>
      <property name="Name" column="aname" />
      <map name="Items" cascade="all-delete-orphan">
         <key column="a_id" />
         <index column="idx" type="String" />
         <one-to-many class="B" />
      </map>      
   </class>
   <class name="B" table="b" lazy="false">
      <id name="Id" column="id" unsaved-value="null">
         <generator class="native" />
      </id>
      <property name="Name" column="aname" />
   </class>
</hibernate-mapping>

Test:
Code:
[Test]
        public void Simple()
        {
            A a = new A();
            a.Name = "first generic type";
            a.Items = new Dictionary<string, B>();
            B firstB = new B();
            firstB.Name = "first b";
            B secondB = new B();
            secondB.Name = "second b";

            a.Items.Add("first", firstB);
            a.Items.Add("second", secondB);

            using (ISession s = NHibernateHelper.GetCurrentSession())
            {
                s.SaveOrUpdate(a);
                s.Flush();

                Assert.IsNotNull(a.Id);
                Assert.IsNotNull(firstB.Id);
                Assert.IsNotNull(secondB.Id);

                var query = from v in s.Linq<A>()
                            where v.Id == a.Id
                            select v;

                a = query.FirstOrDefault();
                B thirdB = new B();
                thirdB.Name = "third B";
                a.Items.Add("third", thirdB);
                Assert.AreEqual(3, a.Items.Count, "3 items in the map now");
                s.Flush();

                a = s.Load<A>(a.Id);
                a.Items["second"] = a.Items["third"];
                s.Flush();
            }
        }


Top
 Profile  
 
 Post subject: Re: NHibernate.Linq last release and lazy loading.......
PostPosted: Mon Jul 27, 2009 3:21 am 
Newbie

Joined: Wed Jul 22, 2009 8:40 am
Posts: 5
I found the code that throw Exeption: (BinaryBooleanReducer.cs line 20)
Code:
private Expression ProcessBinaryExpression(Expression exprToCompare, Expression exprToReturn, ExpressionType nodeType, Expression original)
{
         BooleanConstantFinder visitor = new BooleanConstantFinder();
         visitor.Visit(exprToCompare);

            if (visitor.Constant.HasValue) // visitor.Constant is null
         {
            switch (nodeType)
            {
               case ExpressionType.Equal:
                  return visitor.Constant.Value ? exprToReturn : Expression.Not(exprToReturn);
               case ExpressionType.NotEqual:
                  return visitor.Constant.Value ? Expression.Not(exprToReturn) : exprToReturn;
               case ExpressionType.Or:
               case ExpressionType.OrElse:
                  return visitor.Constant.Value ? Expression.Constant(true) : exprToReturn;
               case ExpressionType.And:
               case ExpressionType.AndAlso:
                  return visitor.Constant.Value ? exprToReturn : Expression.Constant(false);
               default:
                  return original;
            }
         }
         else
            return original;
      }

and in this code (BinaryCriterionVisitor.cs line 148):
Code:
public static ICriterion GetBinaryCriteria(
         ICriteria rootCriteria,
         ISession session,
         BinaryExpression expr,
         ComparePropToValue comparePropToValue,
         ComparePropToProp comparePropToProp,
         CompareValueToCriteria compareValueToCriteria,
         ComparePropToCriteria comparePropToCriteria)
      {
         var left = new BinaryCriterionVisitor(rootCriteria, session);
         var right = new BinaryCriterionVisitor(rootCriteria, session);

         left.Visit(expr.Left);
         right.Visit(expr.Right);

         //the query should have been preprocessed so that
         //only the following combinations are possible:
         // LEFT           RIGHT
         // ========================
         // property       value
         // property       property
         // property       criteria
         // value          criteria
         // criteria       criteria   <== not supported yet

         switch (left.Type)
         {
            case BinaryCriterionType.Property:
               switch (right.Type)
               {
                  case BinaryCriterionType.Value:
                     object val = right.Value;
                     if (left.ConvertTo != null)
                        val = LinqUtil.ChangeType(val, left.ConvertTo);
                     return comparePropToValue(left.Name, val);

                  case BinaryCriterionType.Property:
                     return comparePropToProp(left.Name, right.Name);

                  case BinaryCriterionType.Criteria:
                     return comparePropToCriteria(left.Name, right.Criteria);
               }
               break;

            case BinaryCriterionType.Value:
               return compareValueToCriteria(left.Value, right.Criteria); // right.Criteria = 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.