NHibernate - lazy loading: no session or session was closed - c#

I'm confused by the following NHibernate behaviour:
Domain classes and mappings:
public class Category
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
private IList<Product> _products;
public virtual IList<Product> Products
{
get { return new List<Product>(_products).AsReadOnly(); }
}
public Category()
{
_products = new List<Product>();
}
}
public class Product
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual Category Category { get;set; }
}
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Schema("dbo");
Table("tProducts");
Id(x => x.ID);
Map(x => x.Name);
References(x => x.Category).Column("CategoryID");
}
}
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Schema("dbo");
Table("tCategories");
Id(x => x.ID);
Map(x => x.Name);
HasMany(x => x.Products)
.KeyColumn("CategoryID")
.Access.CamelCaseField(Prefix.Underscore)
.Inverse()
.Cascade.All();
}
}
Code causing trouble:
Category category;
using (var session = sessionFactory.OpenSession())
{
category = session.Get<Category>(1);
}
using (var session = sessionFactory.OpenSession())
{
var products = category.Products; // exception
}
Why do I get no session exception when I'm trying to get products? I've got a session here! How to avoid this exception (I prefer 2 sessions here, and I want to keep loading lazy)?
Thanks in advance!

Can you re-attach the object to your new session either:
ISession.Update(myDetachedInstance);
or, if the object has not been changed:
ISession.Lock(myDetachedInstance, NHibernate.LockMode.None);
For more info, see: http://intellect.dk/post/Detached-objects-in-nHibernate-and-Lazy-loading.aspx

I got this same error and it was due to the WPF UI holding on to references to objects from the old nhibernate session after I clicked the "refresh" button which called refresh on the data context which disassociated all cached objects in the session.
I needed to make sure "NotifyChange" was being fired to make the UI update (and that the UI was listening to it).

Related

Inserting a parent entity with existing child in Fluent NHibernate

This is a general question, I'm sure it's quite common, however I haven't found anything on it (or I don't know what to search for I guess).
I'm having the following entities in my project:
public class User
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual Unit Unit { get; set; }
}
public class Unit
{
public virtual int Id { get; set;}
public virtual string Name { get; set;}
}
This is how I've done the Fluent NHibernate mappings:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id).GeneratedBy.Identity().Column("UserId");
Map(x => x.FirstName);
Map(x => x.LastName);
References(x => x.Unit).Column("UnitId");
}
}
public class UnitMap : ClassMap<Unit>
{
public UnitMap()
{
Table("Unit");
LazyLoad();
Id(x => x.Id).GeneratedBy.Identity().Column("UnitId");
Map(x => x.Name).Column("Name").Not.Nullable();
HasMany(x => x.Users).KeyColumn("UnitId");
}
}
Now here is my question. How do I create a new User if I only have the user's unit Id not a full unit object, and the unit already exists in the database (created previously) ?
Something like this:
public class TestClass
{
// Adding a user to a unit example
public void SavingAUser(int unitId)
{
var user = new User
{
FirstName = "TestFirstName",
LastName = "TestLastName",
Unit = new Unit() // <-- I have only the Id of the unit I don't actually have a unit object here I don't want to query the DB to get the full object, I already have the Id
};
var userRepository = new UserRepository();
userRepository.Save(user);
}
}
How would I go about something like this. I hope I'm making sense, if not please let me know I'll throw in more clarifications. I'm also pretty certain that this is a very common scenario
You can return a proxy to the unit without fetching it.
var user = new User
{
FirstName = "TestFirstName",
LastName = "TestLastName",
Unit = Session.Load<Unit>(unitId)
}
You'll need to expose the session object.

Fluent nhibernate one-to-many

I have a one-to-many relationship and cannot get cascade to work, once I set cascade I just get "object references an unsaved transient instance ...".
My mapping looks like this
public class SharedDetailsMapping : ClassMap<SharedDetails>
{
public SharedDetailsMapping()
{
Id(x => x.Id).GeneratedBy.Identity();
HasMany(x => x.Foos);
}
}
public class FooMapping : ClassMap<Foo>
{
public FooMapping()
{
Id(x => x.Id).GeneratedBy.Identity();
References(x => x.SharedDetails).Cascade.SaveUpdate();
}
}
The classes like this
public class Foo
{
public Foo()
{
SharedDetails = new SharedDetails();
SharedDetails.Foos.Add(this);
}
public Foo(SharedDetails sharedDetails)
{
SharedDetails = sharedDetails;
SharedDetails.Foos.Add(this);
}
public virtual Guid Id { get; set; }
public virtual SharedDetails SharedDetails { get; set; }
}
public class SharedDetails
{
public SharedDetails()
{
Foos = new List<Foo>();
}
public virtual Guid Id { get; set; }
public virtual IList<Foo> Foos { get; set; }
}
I then want to create Foos without having to save the SharedDetails first if its a new Foo, like so:
using (var transaction = _session.BeginTransaction())
{
var shared = new SharedDetails();
var fooOne = new Foo(shared);
_session.SaveOrUpdate(fooOne);
var fooTwo = new Foo(shared);
_session.SaveOrUpdate(fooTwo);
transaction.Commit();
}
Cannot figure out what I have done wrong, it works ofcurse if I save the SharedDetails first but that is why I have Cascade setup.
In your SharedDetailsMapping, modify your HasMany to add .Inverse():
public class SharedDetailsMapping : ClassMap<SharedDetails>
{
public SharedDetailsMapping()
{
Id(x => x.Id).GeneratedBy.Identity();
HasMany(x => x.Foos)
.Inverse();
}
}
This instructs NHibernate that Foo owns the relationship, which will help it save objects in the relationship in the right order (in this case, the SharedDetails has to get saved first so we have its ID when Foo is saved).
Further info on the purpose/when to use Inverse: NHibernate's inverse - what does it really mean?
The TL;DNR version:
If you have a bidirectional relationship in your classes (HasMany on one side, References on the other), the HasMany should have .Inverse().

NHibernate not loading child objects

I have following class and associated mappings (Fluent NHibernate):
public class Category
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual IList<Category> ChildCategories { get; set; }
}
Mappings:
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("Categories");
Id(x => x.Id).Column("Id").CustomType("Int32").Access.Property()
.CustomSqlType("int").Not.Nullable().Precision(10)
.GeneratedBy.Identity();
Map(x => x.Description).Column("Description").Nullable()
.Generated.Never().CustomType(typeof (string)).Access
.Property().Length(250);
Map(x => x.Name).Not.Nullable().Generated.Never().CustomType("string")
.Access.Property().Column("Name").Length(50);
References(x => x.ParentCategory).Column("ParentCategoryId");
HasMany(x => x.ChildCategories).KeyColumn("ParentCategoryId").Inverse()
.AsBag().Fetch.Select();
}
}
I creating two Category object as follows:
var c = new Category
{
Name = "Ebooks",
Description = "Contains awz, mobi, pdf, epub and other
electronic books"
};
var cc = new Category
{
Name = "Kindle Books",
Description = "Contains Kindle ebook reader format books
(awz, mobi)",
ParentCategory = c
};
session.SaveOrUpdate(c);
session.SaveOrUpdate(cc);
When I try to access saved objects:
var c = session.Load<Category>(1);
var cc = c.ChildCategories;
c contains the Category object with Id of 1 but its ChildCategories property is null.
What I am doing wrong?
PS:- This is my first experiment with NHibernate and so with Fluent NHibernate.
EDIT:- The following stuff worked. I needed to close the session before opening it again for reading. Otherwise it read just from memory nad as #Holf has pointed out I needed to add Child Category to Category like:
c.ChilCategories.Add(cc);
I just did as follows:
var session = sf.OpenSession();
CreateCategory(session);//ADDED TWO CATEGORIES EBooks, Kindle Ebooks
session.Close();
session = sf.OpenSession();
FetchCategories(session);//READ CATEGORY EBooks AND ASSOCIATED CHILDREN
session.Close();
Although you've handled one side of the relationship, by assigning 'c' as the ParentCategory of 'cc', I can't see anywhere that you've done the inverse.
I think you'll also need to do
c.ChildCategories.Add(cc);
before doing the SaveOrUpdate.
Can you update your map to the following:
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("Categories");
Id(x => x.Id).Column("Id").CustomType("Int32").Access.Property()
.CustomSqlType("int").Not.Nullable().Precision(10)
.GeneratedBy.Identity();
Map(x => x.Description).Column("Description").Nullable()
.Generated.Never().CustomType(typeof (string)).Access
.Property().Length(250);
Map(x => x.Name).Not.Nullable().Generated.Never().CustomType("string")
.Access.Property().Column("Name").Length(50);
References(x => x.ParentCategory).Column("ParentCategoryId");
HasMany(x => x.ChildCategories).KeyColumn("ParentCategoryId").Inverse()
.Cascade.All();
}
}
Also you cannot be sure that the Id with value 1 refers to the Parent Category, use the LInq provider to load the correct object.

NHibernate: How is identity Id updated when saving a transient instance?

If I use session-per-transaction and call:
session.SaveOrUpdate(entity) corrected:
session.SaveOrUpdateCopy(entity)
..and entity is a transient instance with identity-Id=0. Shall the above line automatically update the Id of the entity, and make the instance persistent? Or should it do so on transaction.Commit? Or do I have to somehow code that explicitly?
Obviously the Id of the database row (new, since transient) is autogenerated and saved as some number, but I'm talking about the actual parameter instance here. Which is the business logic instance.
EDIT - Follow-up, of related problem.
Mappings:
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name);
HasMany(x => x.Staff) // 1:m
.Cascade.All();
HasManyToMany(x => x.Products) // m:m
.Cascade.All()
.Table("StoreProduct");
}
}
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.FirstName);
Map(x => x.LastName);
References(x => x.Store); // m:1
}
}
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name).Length(20);
Map(x => x.Price).CustomSqlType("decimal").Precision(9).Scale(2);
HasManyToMany(x => x.StoresStockedIn)
.Cascade.All()
.Inverse()
.Table("StoreProduct");
}
}
EDIT2
Class definitions:
public class Store
{
public int Id { get; private set; }
public string Name { get; set; }
public IList<Product> Products { get; set; }
public IList<Employee> Staff { get; set; }
public Store()
{
Products = new List<Product>();
Staff = new List<Employee>();
}
// AddProduct & AddEmployee is required. "NH needs you to set both sides before
// it will save correctly"
public void AddProduct(Product product)
{
product.StoresStockedIn.Add(this);
Products.Add(product);
}
public void AddEmployee(Employee employee)
{
employee.Store = this;
Staff.Add(employee);
}
}
public class Employee
{
public int Id { get; private set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Store Store { get; set; }
}
public class Product
{
public int Id { get; private set; }
public string Name { get; set; }
public decimal Price { get; set; }
public IList<Store> StoresStockedIn { get; private set; }
}
As far as your question is concerned, whenever you flush your session is when your entity is persisted to the database. When saving your (new) entity, NHibernate generates the ID for you using the generator you provided.
Keep in mind that an Identity generator is not recommended (see this post by Ayende).
When you use an Identity generator, your new entity is persisted to the database when you save, even if you don't flush to the database. The reason this happens is because NHibernate needs to provide you with an ID for the entity, which it can't do without doing a roundtrip to the database.
A better solution would be to use something like a Guid generator, or HiLo if you want 'normal' values. This way you can save your entity without actually having to do a database roundtrip, which allows you to do a lot more performance wise (batching comes to mind).
I'm not sure I understand your question. The actual saving to the database occurs when the session is flushed (e.g. by committing the transaction). Calling SaveOrUpdate() doesn't itself save the entity, it just informs the session that the entity is due to be saved when the session is flushed.
Assuming that the ID of the entity maps to an identity field in the database and that your mapping tells NHibernate that identity is set by the database, then the ID set by the database will be set as the entity's ID when it is saved.
Nhibernate will set the ID property of your entity just after SaveOrUpdate call.
I noticed I saved by calling:
session.SaveOrUpdateCopy(entity);
..which does NOT update the Id. But by changing to:
session.SaveOrUpdate(entity);
..the Id's of transient entity will be updated.
I probably misunderstood the documentation (?).. Section 9.4.2 says:
SaveOrUpdateCopy(Object o)... If the given instance is unsaved or does not exist in the database, NHibernate will save it and return it as a newly persistent instance.
Is it just me, or does it not sound like a transient object (unsaved), will be "returned as persistent" ? Doesn't that mean with updated Id? Would appreciate a clarification how to interpret this sentence correctly (?)

What are some reasons NHibernate will not execute SQL?

I am using Fluent NHibernate to map entities and am having a problem getting a repository to give a resultset. In the console, the SQL does not show but other repositories do. I have a feeling that it is because of the Mappings but can't tell why. The table name includes an underscore which is one of the only differences between this repo and others. My question is what could cause the sql not to be executed?
Here is my setup.
Entity:
public class Org
{
public virtual int ID { get; set; }
public virtual string IndividualName { get; set; }
public virtual string GroupName { get; set; }
public virtual string AddressLine1 { get; set; }
public virtual string AddressLine2 { get; set; }
}
Mapping:
public class OrgMap : ClassMap<Org>
{
public OrgMap()
{
Table(#"Org_Updates"); // Also tried Table("Org_Updates");
Map(x => x.ID);
Map(x => x.IndividualName);
Map(x => x.GroupName);
Map(x => x.AddressLine1, "PhysicalLocationAddress");
Map(x => x.AddressLine2, "PLAddr2");
Repository:
public class OrgRepository : RepositoryBase<Org>, IOrgRepository
{
public IList<Org>GetTop50()
{
var query = All().AsList();
return query;
}
}
RepositoryBase:
public class OrgRepositoryBase<T> : RepositoryBase<T> where T : class
{
public OrgRepositoryBase()
{
var registry = ServiceLocator.Current.GetInstance<EventListenerRegistry>();
registry.RegisterListenerForType<T>(GetType(), EventType.Save);
registry.RegisterListenerForType<T>(GetType(), EventType.Delete);
}
protected override ISession GetSession()
{
return UnitOfWork.Current.GetSessionFromContext<ISession>(typeof (OrgModel));
}
protected override Type ModelType
{
get { return typeof (OrgModel); }
}
}
}
As I said before, the other repositories that use other entites/mapping work. I can use this repository, exchanging the entity/mapping that it implements and it will work. I'm pretty sure it's because of hte mapping but can't tell which part. I have checked the table name and the column names.
Thanks for the help
I am making an assumption here that your table does have a primary key. If so, should you not map that primary key
public class OrgMap : ClassMap<Org>
{
public OrgMap()
{
Table(#"Org_Updates");
Id(x => x.ID);
Map(x => x.IndividualName);
Map(x => x.GroupName);
Map(x => x.AddressLine1, "PhysicalLocationAddress");
Map(x => x.AddressLine2, "PLAddr2");

Categories