NHibernate:非法尝试将集合与两个打开的会话相关联
NHibernate: Illegal attempt to associate a collection with two open sessions
我在使用 NHibernate 时遇到了问题,首先声明我不是以英语为母语的人,好吧,让我们进入正题。
当我遇到这个错误时,我在网上搜索了一下,看到很多人都在谈论它,但是他们中的大多数人都对 cascade 属性有疑问,如果我想说,完全不符合我的情况,甚至如果是我就打不通了。
我的数据库很小,但有点复杂。
我的模型文件是使用 Entity Developer 生成的。
如您所见,存在继承、一对多和多对多关系,
因为我是 NHibernate 的新手,所以我写了一个测试代码,我的正常测试成功了,直到我添加了这个多对多关系船。
所以我写了这段代码:
//Owner o = new Owner
//{
// //OwnerId = Guid.NewGuid(),
// CellPhone1 = null,
// CellPhone2 = "09132198895",
// Phone = "03114335502",
// Firstname = "Hassan",
// Lastname = "Faghihi",
// OwnerTitle = PersonTitle.Mr
//};
//OwnerFactory ownerFactory = new OwnerFactory();
//ownerFactory.SaveOwner(o);
//SellHouse sh = new SellHouse
//{
// //ItemId = Guid.NewGuid(),
// Owner = o,
// Address = "dsasd",
// BuildYear = 1393,
// City = "Isfahan",
// Code = "A-512",
// Country = "Iran",
// Description = "Nothing",
// Dimensions = "4X5",
// Directions = Directions.North | Directions.South,
// Document = "dasd",
// Exchange = true,
// Facilities = Facilities.Elevator | Facilities.KichenMdfService | Facilities.Parking | Facilities.Warehouse,
// Floor = 4,
// HasYard = false,
// HouseType = HouseType.Apartment,
// IssueDate = DateTime.Now,
// Loan = 3124,
// Meter = 130,
// NumberOfBlocks = 4,
// NumberOfFloors = 0,
// OtherFacilities = "Nothing",
// Rooms = 2,
// ShareOfSixPart = 4.2f,
// State = "Isfahan",
// District = "kaveh",
// ExchangeDescription = "",
// Images = null,
// IsRented = false,
// Maps = null,
// MortgagePayed = 0,
// Price = 2222222,
// RentAmount = 0,
// Substructure = 4,
//};
GalleryFactory galleryFactory = new GalleryFactory();
Gallery g = new Gallery
{
Image = Properties.Resources.jeans_texture03.ToByte()
};
galleryFactory.SaveGallery(g);
SellHouseFactory sellHouseFactory = new SellHouseFactory();
//factory.SaveSellHouse(sh);
HashSet<Gallery> galleries = new HashSet<Gallery>();
galleries.Add(g);
SellHouse sellHouse = sellHouseFactory.GetSellHouses().FirstOrDefault();
if (sellHouse != null)
{
comboBox1.SelectedIndex = (int)sellHouse.Owner.OwnerTitle;
textBox1.Text = sellHouse.Owner.Firstname+sellHouse.Owner.Lastname;
//sellHouse.Images = galleries;
sellHouseFactory.SaveSellHouse(sellHouse);
}
我创建一个Owner对象,然后保存,传给sellHouse,并将Images和Maps(我的多对多关系)设置为null。
所以 sellHouse 创建了。
然后我想知道如何将图像或地图添加到我的 sellHouse,
所以我将包含一个画廊元素的列表传递给 sellHouse Maps 属性。
它生成 "Illegal attempt to associate a collection with two open sessions",首先我虽然原因是因为我的画廊在我将它传递给卖房子之前没有保存,所以我按照你在我的代码中看到的那样做了,并手动完成了。
但它仍然继续产生该错误...
我提供了所需的文件,您可以设置一个示例并更好地理解我的代码。
我非常渴望听到您的回答,因为我对 Hibernate 和 NHibernate 知之甚少。
DropBox 中的来源:
可能在您的所有工厂方法中您都打开了一个新会话。
但是你必须在每个线程(或事务)中只打开一个会话。
我在使用 NHibernate 时遇到了问题,首先声明我不是以英语为母语的人,好吧,让我们进入正题。
当我遇到这个错误时,我在网上搜索了一下,看到很多人都在谈论它,但是他们中的大多数人都对 cascade 属性有疑问,如果我想说,完全不符合我的情况,甚至如果是我就打不通了。
我的数据库很小,但有点复杂。
我的模型文件是使用 Entity Developer 生成的。
如您所见,存在继承、一对多和多对多关系,
因为我是 NHibernate 的新手,所以我写了一个测试代码,我的正常测试成功了,直到我添加了这个多对多关系船。
所以我写了这段代码:
//Owner o = new Owner
//{
// //OwnerId = Guid.NewGuid(),
// CellPhone1 = null,
// CellPhone2 = "09132198895",
// Phone = "03114335502",
// Firstname = "Hassan",
// Lastname = "Faghihi",
// OwnerTitle = PersonTitle.Mr
//};
//OwnerFactory ownerFactory = new OwnerFactory();
//ownerFactory.SaveOwner(o);
//SellHouse sh = new SellHouse
//{
// //ItemId = Guid.NewGuid(),
// Owner = o,
// Address = "dsasd",
// BuildYear = 1393,
// City = "Isfahan",
// Code = "A-512",
// Country = "Iran",
// Description = "Nothing",
// Dimensions = "4X5",
// Directions = Directions.North | Directions.South,
// Document = "dasd",
// Exchange = true,
// Facilities = Facilities.Elevator | Facilities.KichenMdfService | Facilities.Parking | Facilities.Warehouse,
// Floor = 4,
// HasYard = false,
// HouseType = HouseType.Apartment,
// IssueDate = DateTime.Now,
// Loan = 3124,
// Meter = 130,
// NumberOfBlocks = 4,
// NumberOfFloors = 0,
// OtherFacilities = "Nothing",
// Rooms = 2,
// ShareOfSixPart = 4.2f,
// State = "Isfahan",
// District = "kaveh",
// ExchangeDescription = "",
// Images = null,
// IsRented = false,
// Maps = null,
// MortgagePayed = 0,
// Price = 2222222,
// RentAmount = 0,
// Substructure = 4,
//};
GalleryFactory galleryFactory = new GalleryFactory();
Gallery g = new Gallery
{
Image = Properties.Resources.jeans_texture03.ToByte()
};
galleryFactory.SaveGallery(g);
SellHouseFactory sellHouseFactory = new SellHouseFactory();
//factory.SaveSellHouse(sh);
HashSet<Gallery> galleries = new HashSet<Gallery>();
galleries.Add(g);
SellHouse sellHouse = sellHouseFactory.GetSellHouses().FirstOrDefault();
if (sellHouse != null)
{
comboBox1.SelectedIndex = (int)sellHouse.Owner.OwnerTitle;
textBox1.Text = sellHouse.Owner.Firstname+sellHouse.Owner.Lastname;
//sellHouse.Images = galleries;
sellHouseFactory.SaveSellHouse(sellHouse);
}
我创建一个Owner对象,然后保存,传给sellHouse,并将Images和Maps(我的多对多关系)设置为null。 所以 sellHouse 创建了。 然后我想知道如何将图像或地图添加到我的 sellHouse, 所以我将包含一个画廊元素的列表传递给 sellHouse Maps 属性。 它生成 "Illegal attempt to associate a collection with two open sessions",首先我虽然原因是因为我的画廊在我将它传递给卖房子之前没有保存,所以我按照你在我的代码中看到的那样做了,并手动完成了。 但它仍然继续产生该错误...
我提供了所需的文件,您可以设置一个示例并更好地理解我的代码。
我非常渴望听到您的回答,因为我对 Hibernate 和 NHibernate 知之甚少。
DropBox 中的来源:
可能在您的所有工厂方法中您都打开了一个新会话。 但是你必须在每个线程(或事务)中只打开一个会话。