Open
Description
Fabrizio Gennari created an issue — 28th November 2012, 17:35:17:
This program
namespace NhibernateExperiments { using System.Collections.Generic; using System.Reflection; using log4net.Config; using NHibernate; using NHibernate.Cfg; public class Program { public static void Main(string[] args) { BasicConfigurator.Configure(); Configuration configuration = new Configuration().SetProperties(new Dictionary<string, string> { {Environment.ConnectionDriver, "NHibernate.Driver.SQLite20Driver"}, {Environment.ConnectionString, "Data Source=h:\\testdb;Version=3"}, {Environment.Dialect, "NHibernate.Dialect.SQLiteDialect"}, {Environment.QuerySubstitutions, "true=1;false=0"}, {Environment.ShowSql, "true"}, }).AddAssembly(Assembly.GetExecutingAssembly()); ISessionFactory buildSessionFactory = configuration.BuildSessionFactory(); ISession session = buildSessionFactory.OpenSession(); IEnumerable<CarDealer> carDealers = session.QueryOver<CarDealer>().Future(); foreach (CarDealer carDealer in carDealers) { session.Refresh(carDealer); } } } }
is causing a HibernateException "collection is not associated with any session".
Mapping file<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="nhibernateExperiments" namespace="NhibernateExperiments"> <class name="CarDealer" lazy="false"> <id name="id" column="id" access="field"> <generator class="native"/> </id> <property name="Name" /> <set name="models" table="ModelInStock" lazy="false" access="field"> <key column="dealerid"/> <many-to-many class="CarModel" column="modelid"/> </set> </class> <class name="CarModel" lazy="false"> <id name="id" column="id" access="field"> <generator class="native"/> </id> <property name="Name" /> <property name="Year" /> <set name="dealers" table="ModelInStock" lazy="false" access="field"> <key column="modelid"/> <many-to-many class="CarDealer" column="dealerid"/> </set> </class> </hibernate-mapping>
Objects
namespace NhibernateExperiments { using System.Collections.Generic; using Iesi.Collections.Generic; public class CarModel { private readonly Iesi.Collections.Generic.ISet<CarDealer> dealers = new HashedSet<CarDealer>(); private int id; public CarModel(string name, int year) { Year = year; Name = name; } private CarModel() { } public string Name { get; private set; } public int Year{ get; private set; } } public class CarDealer { private readonly Iesi.Collections.Generic.ISet<CarModel> models; private int id; public CarDealer(string name) { this.Name = name; this.models = new HashedSet<CarModel>(); } private CarDealer() { } public string Name { get; private set; } public IEnumerable<CarModel> Models { get { return this.models; } } public void AddModel(CarModel model) { this.models.Add(model); } } }
DB schema
CREATE TABLE CarDealer ( id integer primary key autoincrement, Name varchar(50) ) CREATE TABLE CarModel ( id integer primary key autoincrement, Name varchar(50), Year int ) CREATE TABLE ModelInStock ( modelid integer, dealerid integer, foreign key (dealerid) references CarDealer(id), foreign key (modelid) references CarModel(id) )
Fabrizio Gennari added a comment — 28th November 2012, 17:37:16:
Sorry, type should be "bug", not "patch"
Fabrizio Gennari added a comment — 29th November 2012, 10:57:42:
Workaround is to change the foreach loop
foreach (CarDealer carDealer in carDealers) { IPersistentCollection collection = carDealer.Models as IPersistentCollection; if (collection != null) { collection.ForceInitialization(); } session.Refresh(carDealer); }