-->
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.  [ 11 posts ] 
Author Message
 Post subject: Using filters in combination with generic collections
PostPosted: Wed Jan 11, 2006 8:43 pm 
Newbie

Joined: Wed Jan 11, 2006 7:01 pm
Posts: 2
Hi,

Currently I'm working on a project where I decided to use NHibernate for building the persistence layer, basically things are working quite well, and I think it is saving lots of work already :). Because NHibernate does not have native support for generic collections yet I’m using ayende's add-on as a workaround.

The problem is that while using ayende's add-on, the Session.Filter() on persistent collections doesn't seem to work (throwing an NHibernate.QueryException "the collection was unreferenced"). Probably this is because the real persistent collection is wrapped by an NHibernate.generics.EntitySet. Without ayende's add-on the filter is working perfectly.


The code I’m using is:
Code:
Person person = (Person)NHibernateSessionManager.CurrentSession.Get(typeof(Person),1);
ICollection contacts = NHibernateSessionManager.CurrentSession.Filter(person.Contacts,"where this.Id = 2");
(NHibernateSessionManager is just an IHttpModule for controlling the hibernate session to make sure only one session is opened, and is properly closed)


Do I miss something? Or is there something I could do about this like implementing an specific interface or something?

Thanks in advance,
Patrick


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 2:00 am 
Beginner
Beginner

Joined: Mon Jan 09, 2006 8:02 am
Posts: 22
I am also Looking at Something Similar but Getting some Exceptions. Could u plz let me Know if ur Solution Worked...


Thanking You

Prasanth


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 2:51 pm 
Newbie

Joined: Wed Jan 11, 2006 7:01 pm
Posts: 2
Hi Prasanth,

actually I didn't found a real satisfactory solution yet. I'm now doing queries using a Criteria object, which is also working well, but just not really what I was looking for.

Thus now using something like.
Code:
ICriteria crit = NHibernateManager.CurrentSession.CreateCriteria(typeof(Relation));
crit.Add(Expression.Eq("ParentId",1));
crit.Add(Expression.Eq("Street","Something"));
ICollection contacts = crit.List();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 8:16 pm 
Beginner
Beginner

Joined: Thu Dec 08, 2005 6:49 pm
Posts: 49
I posted the same problem before realising that you'd already started discussing the issue (http://forum.hibernate.org/viewtopic.php?t=954292).

It seems to be because Ayende's method involves wrapping the ISet and NHibernate doesn't know how to pass through the EntitySet<T> to get to it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 2:08 pm 
Newbie

Joined: Wed Oct 25, 2006 2:05 pm
Posts: 6
Exactly same bug with new NHibernate 1.2 beta 1. Very annoying.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 7:26 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
You dont need Ayende's method with 1.2 beta. The built-in generic support works well.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 4:52 am 
Newbie

Joined: Wed Oct 25, 2006 2:05 pm
Posts: 6
grennis wrote:
You dont need Ayende's method with 1.2 beta. The built-in generic support works well.

I'm not using Ayende's NHibernate.Generics with 1.2b. Here is simple testcase, that fails with NHibernate.QueryException: the collection was unreferenced.

[TestMethod]
public void TestHibernateFiltering() {
Configuration cfg = new Configuration();
cfg.AddAssembly("Tests");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession s = factory.OpenSession();
try {
IQuery q = s.CreateQuery("from BusinessEntity");
IList<BusinessEntity> list = q.List<BusinessEntity>();
Console.WriteLine(list.Count);
q = s.CreateFilter(list, "where this.IsArchived = false");
// this line fails
Console.WriteLine(q.List<BusinessEntity>().Count);
} finally {
s.Close();
}
}

// the class is trivial

public class BusinessEntity {
private Guid id;
private bool isArchived;

public BusinessEntity() {}

public virtual Guid ID {
get { return id; }
set { id= value; }
}

public virtual bool IsArchived {
get { return isArchived; }
set { isArchived = value; }
}
}

Stack trace is:
at NHibernate.Impl.SessionImpl.GetFilterTranslator(Object collection, String filter, QueryParameters parameters, Boolean scalar)
at NHibernate.Impl.SessionImpl.Filter(Object collection, String filter, QueryParameters parameters, IList results)
at NHibernate.Impl.SessionImpl.Filter[T](Object collection, String filter, QueryParameters parameters)
at NHibernate.Impl.QueryFilterImpl.List[T]()

Any help with it is really appreciated )

BTW: it fails even when using non-generic IQuery.List() method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 5:23 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You can't use CreateFilter on arbitrary collections, only on those that belong to entities (persistent collections).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 5:34 am 
Newbie

Joined: Wed Oct 25, 2006 2:05 pm
Posts: 6
sergey wrote:
You can't use CreateFilter on arbitrary collections, only on those that belong to entities (persistent collections).

What are you talking about? I'm getting some entities to persistent collection, then filtering it. Or I've missed something? Please, explain.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 6:06 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Persistent collections are those collections that NHibernate tracks changes made to. They always belong to an entity, like LineItems collection of Order.

ILists and IList<T>'s returned from queries are not persistent collections.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 6:13 am 
Newbie

Joined: Wed Oct 25, 2006 2:05 pm
Posts: 6
sergey wrote:
Persistent collections are those collections that NHibernate tracks changes made to. They always belong to an entity, like LineItems collection of Order.

ILists and IList<T>'s returned from queries are not persistent collections.

Thanks, Sergey, that's explains all )


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