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.  [ 5 posts ] 
Author Message
 Post subject: hibernate.linq query for a many-to-many association
PostPosted: Tue Dec 16, 2008 6:30 am 
Newbie

Joined: Wed Jan 09, 2008 9:31 am
Posts: 8
Hello i've already performed an extensive search on the forums and by the search engines. This is not a bug with nhibernate but a problem on how to implement a particular query.
Hibernate version: 1.2.1.4000
Name and version of the database you are using: SQLite Ado.Net (latest, version: 1.0.60.0)
Code between sessionFactory.openSession() and session.close():
I'd like to know that.

My scenario is as follows, there is a customer order relation, for this i use a list property with the following mapping definition.
Code:
class Customer {
        [NHibernate.Mapping.Attributes.List(0,
            Name = "AssociatedOrders",
            Table = "MM_Customer_Order",
            Lazy = false,
            Cascade = NHibernate.Mapping.Attributes.CascadeStyle.SaveUpdate,
        [NHibernate.Mapping.Attributes.Key(1, Column = "Id_Customer")]
        [NHibernate.Mapping.Attributes.Index(2, Column = "Ordering_Index")]
        [NHibernate.Mapping.Attributes.ManyToMany(3, Column = "Id_Order", Class = "MyTest.Order, MyTest",
            Lazy = NHibernate.Mapping.Attributes.RestrictedLaziness.False)]
        public virtual IList<Order> AssociatedOrders
        {
         get;set;
   }
}

So nhibernate uses three tables for this: Customers, Orders, and MM_Customer_Order reflecting the relation of both with an additional column "Ordering_Index" determining the sequence.

In my model the following may be true: An Order item may be associated to several customers.

So to my question: how does a query look like that selects all customers having a particular order item contained in AssociatedOrders?

If i'd select all customers
Code:
var query = from customer in session.Linq<Customer>()
                  select customer;

i could scan through all via foreach to find the particular orders, which is not what i want.
A query like
Code:
            var query = from customer in session.Linq<Customer>()
                        from o in session.Linq<Order>()
                        where customer.AssociatedOrders.Contains(order)
                        select customer;
            resultSet = query.ToList();

results in a null pointer exception, probably since the collection is not yet initialized;
Code:
   bei NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWholeAssociationPath(Subcriteria subcriteria)
   bei NHibernate.Loader.Criteria.CriteriaQueryTranslator.CreateAssociationPathCriteriaMap()
   bei NHibernate.Loader.Criteria.CriteriaQueryTranslator..ctor(ISessionFactoryImplementor factory, CriteriaImpl criteria, Type rootEntityName, String rootSQLAlias)
   bei NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, Type rootEntityName, IDictionary enabledFilters)
   bei NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria, IList results)
   bei NHibernate.Impl.SessionImpl.Find[T](CriteriaImpl criteria)
   bei NHibernate.Impl.CriteriaImpl.List[T]()
   bei NHibernate.Linq.NHibernateLinqQuery`1.get_Result()
   bei NHibernate.Linq.NHibernateLinqQuery`1.GetEnumerator()
   bei System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   bei System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   bei DetermineCustomersHavingOrder(Order order, ISession session)
   [..]

Is there a possibility to create a linq expression tree that is able to generate an sql-query which performs the set operations on the database side, like one would expect?
I'm really looking forward for your kind answers,
thanks in advance,
Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 2:46 pm 
Newbie

Joined: Thu Dec 25, 2008 7:13 am
Posts: 10
Any update on this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 2:47 pm 
Newbie

Joined: Thu Dec 25, 2008 7:13 am
Posts: 10
Any update on this?


Top
 Profile  
 
 Post subject: Re: Any updates on this?
PostPosted: Thu Mar 12, 2009 4:00 am 
Newbie

Joined: Wed Jan 09, 2008 9:31 am
Posts: 8
I've still no idea, i solved the problem via a native sql query.

I think the problem is here (in bold):

Code:
var query = from customer in session.Linq<Customer>()
                        from o in session.Linq<Order>()
[b]                     where customer.AssociatedOrders.Contains(order) [/b]                       
                        select customer;
            resultSet = query.ToList();


Is there maybe another way to solve this?

Regards


Top
 Profile  
 
 Post subject: Re: hibernate.linq query for a many-to-many association
PostPosted: Fri Mar 12, 2010 2:17 pm 
Newbie

Joined: Fri Mar 12, 2010 2:00 pm
Posts: 1
I think I've had similar issues and I had, too, to use native SQL queries :-(


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