3

I have a problem saving in session entities created for the very first time...

Each entity has a list of steps, and each step a list of values. (I'm using inverses on mappings). Also entities and steps references to master values already in db. So there is a kind of mix of old and new objects.

When I do the first Save I do Session.Save(entity) and the whole tree is saved in database.

The issue relies in that profiler warns of messages like

Unable to determine if StepValueEntity with assigned identifier ede6a5ee-b4bd-4f67-9c64-11ef85b7d6ff is transient or detached; querying the database. Use explicit Save() or Update() in session to prevent this.

because nhibernate does lots of updates before start to really insert things.

What I'm doign wrong?

I've tried something like iterate each step and value and explicity save it but the same is happening.

Edit:

This is how I do mapping for identity columns, maybe this doesn't give a clue to nhibernate to know about what are new and already persisted entities and I should do it in another way.

Id(x => x.Id).Column("GUID_PIPELINE_STEP_PARAMETER").GeneratedBy.Assigned();

Regards

guillem
  • 2,768
  • 2
  • 30
  • 44
  • Have you done this operation in transaction scope? – Anton Mar 18 '12 at 13:54
  • have you seen this answer? http://stackoverflow.com/a/4804382/537913 – J. Ed Mar 19 '12 at 11:30
  • @sJhonny I already saw this, but to me in this case it doesn't appply because I'm telling nhibernate that I'm using assigned guid in mapping. I will put mapping in body for more details. – guillem Mar 19 '12 at 14:20
  • .. As far as I can understand, the answer I linked to also talks about an assigned Id. Am I missing something? – J. Ed Mar 19 '12 at 17:18
  • Hi @sJhonny, to me is like in this case http://stackoverflow.com/a/5348952/351975 where as my application is responsible for generating Guid there is never a default value to tell nhiberante that this is a non saved entity. – guillem Mar 19 '12 at 17:56

2 Answers2

2

Maybe you've forgot about transaction, look at: http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions

using (var session = sessionFactory.OpenSesion())
using (var tx = session.BeginTransaction())
{
    // perform your insert here
    tx.Commit();
}
Anton
  • 1,583
  • 12
  • 17
  • Master objects where loaded in another transaction, but I always use transactions. So I guess I would discard this as the cause. – guillem Mar 18 '12 at 18:56
0

For the record I did this same question to nhusers group and some people came with ideas about how to focus on this topic.

http://groups.google.com/group/nhusers/browse_thread/thread/bcc962d861c7f111

The most recommended option, and the one I choose, has been not to use auto generated ID and use one generator in mapping. This way NH knows easily that an entity with a blank ID never has been inserted.

I choose the Guid comb generator that creates an unique GUID which solves the fragmented index issue

guillem
  • 2,768
  • 2
  • 30
  • 44