Your set is lazily loaded. In order to ensure no duplicates are added (the
only reason for using a set), the collection must be initialized. If the
session is closed when this happens, you get the exception.

So, it's a matter of session management or domain design. If you don't need
to enforce uniqueness on the collection, you should switch to bag.

If you need the unique guarantee, then stay with set, but be sure the
session is still open when you initialize the collection. (and beware of
Select N+1 issues)

- Jason


On Wed, Sep 22, 2010 at 2:32 PM, José F. Romaniello
<[email protected]>wrote:

> you have a problem with your session management/scope
>
> 2010/9/22 Allan Fagner <[email protected]>
>
> Fabio,
>>
>> I have tried that already. It solves the problem related to the queries.
>> But it creates another, when loading a Catalog with no Product associated to
>> it an exception is raised:
>>
>> "Initializing[Unavailable#]-failed to lazily initialize a collection, no
>> session or session was closed"
>>
>> That's why kept the bag. If there's a way to workaround that, <set/> will
>> fit.
>>
>> Thanks.
>> --
>> Allan Ferreira
>> M: +55-31-91459275
>> E: [email protected]
>>
>>
>> On Wed, Sep 22, 2010 at 12:43 PM, Fabio Maulo <[email protected]>wrote:
>>
>>> What happen to the table and to the queries if you use <set> instead
>>> <bag> ?
>>>
>>> On Wed, Sep 22, 2010 at 11:19 AM, Allan Fagner <[email protected]>wrote:
>>>
>>>> Hi people,
>>>>
>>>> I've been using NHibernate for awhile and now I'm facing a problem which
>>>> I didn't find an answer so far.
>>>>
>>>> The thing is, I have a many-to-many relationship. Everything is working
>>>> the way they suppose to (insert, update, delete and search). The only thing
>>>> bugging me is that every time I update the list of Products (insert and/or
>>>> delete) and save the Catalog, all records in the intermediate table
>>>> (CatalogProduct) get deleted and then (re)inserted. For example: let's say 
>>>> I
>>>> have a Catalog and this catalog is associated with three Products (1,2 and
>>>> 3). If I add a fourth Product to the Catalog the three products already
>>>> there are deleted from CatalogProduct table and the four products (1,2,3 
>>>> and
>>>> 4) are then inserted.
>>>>
>>>> Here are the mappings:
>>>>
>>>> <class name="Catalog" table="Catalog" lazy="true">
>>>>
>>>>
>>>>     <id column="Id" name="Id" access="field.camelcase" type="Int32" 
>>>> unsaved-value="0">
>>>>
>>>>
>>>>
>>>>
>>>>       <generator class="identity"/>
>>>>
>>>>
>>>>
>>>>
>>>>     </id>
>>>>
>>>>     <property column="Name" access="property" type="String" name="Name" 
>>>> not-null="true" length="250" />
>>>>
>>>>
>>>>
>>>>
>>>>     <property column="Code" access="property" type="Int32" name="Code" 
>>>> not-null="true" />
>>>>
>>>>
>>>>
>>>>
>>>>     <property column="Description" access="property" type="String" 
>>>> name="Description" not-null="true" length="250" />
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     <bag name="products" table="CatalogProduct" lazy="true" access="field" 
>>>> generic="true" cascade="save-update" inverse="false">
>>>>
>>>>
>>>>
>>>>
>>>>       <key column="IdCatalog"></key>
>>>>
>>>>
>>>>
>>>>
>>>>       <many-to-many class="Product" column="IdProduct"></many-to-many>
>>>>
>>>>
>>>>
>>>>
>>>>     </bag>
>>>>
>>>>   </class>
>>>>
>>>> <class name="Product" table="Product" lazy="true">
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     <id column="Id" name="Id" access="field.camelcase" type="Int32" 
>>>> unsaved-value="0">
>>>>
>>>>
>>>>
>>>>
>>>>       <generator class="identity"/>
>>>>
>>>>
>>>>
>>>>
>>>>     </id>
>>>>
>>>>     <property column="Sku" access="property" type="String" name="Sku" 
>>>> not-null="true" length="250" />
>>>>
>>>>
>>>>
>>>>
>>>>     <property column="Description" access="property" type="String" 
>>>> name="Description" not-null="true" length="250" />
>>>>
>>>>
>>>>
>>>>
>>>>     <property column="ImageUrl" access="property" type="String" 
>>>> name="ImageUrl" not-null="true" length="250" />
>>>>
>>>>
>>>>
>>>>
>>>>     <property column="Quantity" access="property" type="Int32" 
>>>> name="Quantity" not-null="true" />
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>   </class>
>>>>
>>>>
>>>> This is not a problem for the client as the system is working as it
>>>> should. But, it is bugging me. :)
>>>>
>>>> Any thoughts on how to solve that???
>>>>
>>>> Thanks
>>>> --
>>>> Allan Ferreira
>>>> M: +55-31-91459275
>>>> E: [email protected]
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "nhusers" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<nhusers%[email protected]>
>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/nhusers?hl=en.
>>>>
>>>
>>>
>>>
>>> --
>>> Fabio Maulo
>>>
>>>  --
>>> You received this message because you are subscribed to the Google Groups
>>> "nhusers" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<nhusers%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/nhusers?hl=en.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "nhusers" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<nhusers%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/nhusers?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to