I get a stackoverflow exception when trying to remove a composite object from a collection (but only when i wrap it inside a transaction). However getting the object and deleting it on the session, instead of the collection, and saving the parent works.
The code looks like this:
using (var session = sessionProvider.GetSession())
{
using (var transaction = session.BeginTransaction())
{
var products = session.Query<MyObject>().Select(x => x.MyObjectId > 0).ToList();
//Would fail
var variant = session.Query<MyObject>().FirstOrDefault(x => x.Name == "some name that exists");
var parent = MyObject.Parent;
parent.RemoveMyObject(variant);
session.Save(parent);
//Doesn't fail
//session.Delete(variant);
transaction.Commit();
}
session.Flush();
}
And my mapping looks like this:
HasMany(x => x.MyObject).AsSet().Cascade.AllDeleteOrphan().Inverse().KeyColumn("MyObjectId").BatchSize(2000);
References(x => x.MyObject).Column("MyObjectId");
What should i do to fix this ? It should work in both ways. And it is, as said, only when there's a transaction around. Both approaches works without a transaction.
Best regards
Morten
Related
We have a userobject split between two tables, a person table and user table.
public UserMap()
{
Table("Person");
Id(x => x.PersonId, "PersonId");
Map(x => x.Name, "Name");
Join("User", s => {
s.Fetch.Join();
s.KeyColumn("PersonId");
s.Map(x => x.Username, "Username");
s.Map(x => x.Password, "Password");
});
}
When doing a insert, nHibernate will first insert Name into the Person table, and then Username and Password into the User table.
The problem arise when the insertion into the User-table fails (like trying to insert a user with a username that is already taken). The transaction fails, but the insertion into the Person table is not rolled back.
public User Save(User user)
{
var session = SessionHelper.GetCurrent();
using (var dbTransaction = session.BeginTransaction())
{
try
{
session.SaveOrUpdate(user);
dbTransaction.Commit();
}
catch (Exception)
{
dbTransaction.Rollback();
throw;
}
}
return user;
}
Our SessionFactory is also set up with Fluent NHibernate
public ISessionFactory BuildSessionFactory(string connectionString)
{
return Fluently.Configure().Database(OracleClientConfiguration.Oracle10.ConnectionString(c => c.Is(connectionString))
.Provider<OracleDriverConnectionProvider>()
.Driver<OracleDataClientDriver>()
.ShowSql)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>())
.BuildSessionFactory();
}
I have put the logfile from a failing session in a gist. The User entity is a little more complex in real life, and there are some authentication stuff going on, so the log file want match 1:1 with the painted picture...
Seems like a similar problem was solved here. NHibernate will not revert you changes in the session when you rollback your transaction, i.e if you flush your session after the rollback, you will experience the problems you describe. To ensure no changes in your entities, when you rollback, you have to also close the session.
Add User object in Person Object and Save Person Object only. Use Cascade.All() in Mapping file. or Second option is use TransactionScope.
Instead of closing the session, you can use session.Clear() after you have roleld back the transaction, which will remove all pending writes and make session as good as new.
That did the trick for my application.
I have parent and child objects BOOKLET and DEMOGRAPHICS_INFO in my Oracle database mapped as follows in my data layer:
public BookletMapping()
{
Table("BOOKLET");
Id(x => x.Id, m => m.Column("ID");
...
ManyToOne(x => x.DemographicsInfo, m => m.Column("DEMOGRAPHICS_INFO_ID"));
}
public DemographicsInfoMapping()
{
Table("DEMOGRAPHICS_INFO");
Id(x => x.Id, m => m.Column("ID");
...
}
I have intentionally left out the DemographicsInfo relationship to the Booklet because I don't need to traverse my entities that direction. The ManyToOne relationship will actually be a one-to-one.
I have written a test to ensure I can create a DemographicsInfo and immediately assign it to its parent Booklet, and that looks like this:
[Test]
public void ShouldSaveCorrectEntity()
{
var booklet = _unitOfWork.Get<Booklet>(4);
var demInfo = new DemographicsInfo();
_unitOfWork.Insert(demInfo);
booklet.DemographicsInfo = demInfo;
_unitOfWork.Save();
demInfo.Id.ShouldNotEqual(0);
}
When I call Save(), I get the following exception:
{"ORA-02291: integrity constraint (<schema>.BOOKLET_DEMOGRAPHICS_INFO_FK1) violated - parent key not found\n"}
This is because the demInfo object is not given an Id upon Insert(). My Insert implementation looks like this:
public void Insert<T>(T entity)
{
using (var transaction = _session.BeginTransaction())
{
_session.Save(objectToSave);
_session.Flush();
transaction.Commit();
}
}
Where _session is an NHibernate ISession. Because I have both Saved the new entity (it persists successfully) and Flushed my session, I would expect my demInfo variable to have an Id, but it remains 0, which is a foreign key violation when I try to save my parent object. Am I overlooking a step here? Should I rethink my pattern for adding a new child to an existing parent?
I have solved my issue. As it turns out, the Oracle convention of using Sequences for Id column values and populating them using Triggers on row insert disallows NHibernate from learning the Id upon persisting an entity. I added a Sequence Generator to my Id maps and Insert() now updates the Id on my transient entity upon save so I can assign the DemographicsInfo to my Booklet.DemographicsInfo.
Id(x => x.Id, m =>
{
m.Column("ID");
m.Generator(Generators.Sequence, a => a.Params(new { sequence = "SEQ_TABLE_ID" }));
});
Now my test passes with no exceptions.
I'm trying to get to grips with QueryOver, and I was expecting this to return me a Summary item with its ReportRows collection eagerly loaded.
Update
The first block of code wasn't in my original question but it was the cause of my problem - thanks to dotjoe for the answer
// some code to create a Summary and a whole graph of child records
...
// then...
session.Save(summary);
session.Flush(); // write the changes
session.Evict(summary); // clear out session to check my query below is 'fresh'
// Originally-posted code
Summary summaryAlias = null;
Report reportAlias = null;
var summaryQuery =
session.QueryOver<Summary>(() => summaryAlias)
.Fetch(x => summaryAlias.Reports).Eager
.Left.JoinAlias(() => summaryAlias.Reports, () => reportAlias)
.Where(() => summaryAlias.Id == workItemId);
Summary summary = summaryQuery.SingleOrDefault<Summary>();
session.Close();
However when I hit a breakpoint after session.Close() has been called (to prevent any further lazy loading), I find that everything in my Summary class has been populated, not just the ReportRows collection.
Some examples of things that have been populated even though I wasn't expecting them to be:
ReportRow.Student
ReportRow.Programme
ReportRow.Programme.Modules (a collection)
ReportRow.Programme.Modules.Components (another collection inside each 'Module')
I'm using Fluent automappings, and I've configured it to be lazy-loaded just to be sure using:
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Always())
and also tried...
.Conventions.Add(FluentNHibernate.Conventions.Helpers.LazyLoad.Always())
Why is it loading everything?
Thanks
Could you post the generated hbm.xml? example
Also, try this query and see what happens if you access any lazy properties after the using statements...
Summary summary = null;
using(var session = factory.OpenSession())
using(var tx = session.BeginTransaction())
{
summary = session.QueryOver<Summary>()
//.Fetch(x => x.Reports).Eager
.Where(x => x.Id == workItemId)
.SingleOrDefault<Summary>();
tx.Commit();
}
I'm using NHibernate Search for NHibernate 3.0 GA.
I have this code in my product repository:
public IList<Product> Find(string term)
{
var productFields = new[] { "Name", "Description" };
var importance = new Dictionary<String, float>(2) { { "Name", 4 }, { "Description", 1 } };
var analyzer = new StandardAnalyzer();
var parser = new MultiFieldQueryParser(
productFields,
analyzer,
importance);
var query = parser.Parse(term);
var session = Search.CreateFullTextSession(NHibernateSession.Current);
var tx = session.BeginTransaction();
var fullTextQuery = session.CreateFullTextQuery(query);
fullTextQuery.SetFirstResult(0).SetMaxResults(20);
var results = fullTextQuery.List<Product>();
tx.Commit();
return results;
}
In NH Profiler, I can see that a select statement is issued for every product found. What am I missing?
I found this thread from 2009 but presumably this bug has been fixed.
EDIT [06/06/2011]:
My property mappings as far as associations go are as follows:
mapping.HasManyToMany(c => c.Categories)
.Table("CatalogHierarchy")
.ParentKeyColumn("child_oid")
.ChildKeyColumn("oid")
.Cascade.None()
.Inverse()
.LazyLoad()
.AsSet();
mapping.HasMany(c => c.Variants)
.Table("CatalogProducts")
.Where("i_ClassType=2")
.KeyColumn("ParentOID")
.Cascade.SaveUpdate()
.AsSet();
I don't really want to eager fetch all categories.
As NHibernate.Search is unstable, I would use (and I did) Lucene.NET directly from my application - just to ask Lucene for objects' Ids and then load it using Session.Load().
Also you could change LazyLoad settings at class mapping level, not at relationship level.
Hope it helps.
EDIT: you could specify batch-size for relationships, so they wouldn't cause select N+1 problem
Are you referencing any lazy loaded properties in the product list that is returned by this method? If so, you can change the property mapping to "eager fetch".
I solved this in the end. I realised I had not wrapped the whole thing into a transaction. Once I did, the N+1 issue was gone.
Schoolboy error, but hey, you learn by your mistakes.
When calling Index method on FullTextSession with plain poco object throws the error below, works fine with proxied object.
Stacktrace:
[TransientObjectException: the instance was not associated with this session]
NHibernate.Impl.SessionImpl.GetIdentifier(Object obj) +500
I'm trying to squeeze the performance out of the nhibernate select method I've got the following code:
public virtual IList<T> LoadSearch()
{
return Adapater.Session.QueryOver<T>()
.SelectList(e =>
{
e.Select(x => x.Id);
e.Select(x => x.Title);
e.Select(x => x.Description);
return e;
}).List<object[]>()
.Select(props => new T
{
Id = (Guid)props[0],
Title = (string)props[1],
Description = (string)props[2]
}).ToList();
}
Is there way to return a proxied result? or some how adapt the list to a proxied list?
I think you can only index objects that are associated with a session, i.e. proxied entities.
The plain POCOs you are returning didn't come from NH - so aren't associated with a NH session.
You could try using ISession.Lock(instance, NHibernate.LockMode.None); on each entity to associate it with the session, but I really don't know if that'd work.