-->
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.  [ 8 posts ] 
Author Message
 Post subject: Problems casting PersistentGenericBag to List
PostPosted: Tue May 15, 2007 8:16 pm 
Newbie

Joined: Thu Mar 29, 2007 3:16 pm
Posts: 7
Location: Brasil
NHibernate version: 1.2.0

Full stack trace of any exception that occurs:
Unable to cast object of type
'NHibernate.Collection.Generic.PersistentGenericBag`1[Projeto.Core.Domain.Armazem]' to
type 'System.Collections.Generic.List`1[Projeto.Core.Domain.Armazem]'

Name and version of the database you are using: SQL Server 2005


I have a typed collection and and trying to make it receive a list of persistent entities (mapped as a bag) but I'm getting the error mentioned above.

The error occurs on the following code:

Code:
    private List<Armazem> _armazens;

    public IList<Armazem> Armazens
    {
        set
        {
            _armazens = (List<Armazem>)value;

        }
   }


called by:

Code:

   Usuario usuario = usuarioDao.GetById(codigoUsuario, false);

   view.Armazens = usuario.Armazens;



Excerpt from the Usuario entity mapping:

Code:
<bag name="Armazens" inverse="false" table="usuario_armazem" lazy="true" cascade="delete">
   <key column="usu_id" />
   <many-to-many class="Projeto.Core.Domain.Armazem,Projeto.Core">
      <column name="arm_id" />
   </many-to-many>
</bag> 



Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 1:04 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
What led you to believe that PersistentGenericBag inherits from list? It doesn't.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 8:54 am 
Newbie

Joined: Thu Mar 29, 2007 3:16 pm
Posts: 7
Location: Brasil
Actually, nothing led me into it. I know it doesn't inherits from list.

The problem is: I have this many-to-many relationship mapped as a bag which, as pointed in the documentation, maps to a List. In the entity class the field is declared as an IList and all works well (for testing purposes I changed it to another type of collection and the application failed to initialize).

So, the mapping is correct, the bag is represented as a List in the entity class but how can I access its values? I need to pass the collection to another layer and I'm sure I cannot (or shouldn't) leak NHibernate references through this layer just to use it's generic lists (also, the documentation clearly says not to do that).

Am I missing something?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 4:21 pm 
Newbie

Joined: Thu Mar 29, 2007 3:16 pm
Posts: 7
Location: Brasil
Well, simplifying: how can I access the entities of a bag mapping? I can't use a .NET collection?


Top
 Profile  
 
 Post subject: List
PostPosted: Wed May 16, 2007 4:31 pm 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
Why do you need to cast to a generic list? Just do this:

Code:
private IList<Armazen> _armazens;

public IList<Armazen> Armazens
{
        set
        {
            _armazens = value;

        }
}


assuming that view.Armazens is also an IList<Armazen> ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 5:03 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
As a side note, normal best practice is not to provide a setter for collection properties. Get NHibernate to set the underlying field using a field access strategy and only provide a getter for the property.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 5:47 pm 
Newbie

Joined: Thu Mar 29, 2007 3:16 pm
Posts: 7
Location: Brasil
anortham, merge_s.rottem: this isn't the entity class setter, it's the setter of the _armazens collection of the view object.

I'm using the List collection 'cause I need to use the Find method, which isn't implemented in IList.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 4:17 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi lucashungaro!

You may "wrap" NHibernate collection into BindingList<T> class - either in IList<T> setter (it will hovever ruin lazy capabilities, as BindingList will retreive all collection during construction), either in IList<T> getter - replacing original field with this wrapper - or even via additional property and field (so you'll end with a pair of field/property - one IList<T> for NHibernate and another for your business purposes).

_________________
WBR, Igor


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