0

I have a base class with ID as Primary Key and 3 version numbers.

[NotNull]
public virtual int Id { get; private set; }
[NotNull]
[NotUpdatable]
public virtual int BaseVersion { get; set; }
[NotNull]
[NotUpdatable]
public virtual int MajorVersion { get; set; }
[NotNull]
[NotUpdatable]
public virtual int MinorVersion { get; set; }

Now I want to persist the object again if it gets a new version number or if it does not exist in the database.

foreach (var dataObject in unitOfWork.NewObjects)
{
  if (dataObject.Id > 0)
  {
     _transactionHelper.GetSession().SaveOrUpdate(dataObject.DeepClone());
     continue;
  }

  _transactionHelper.GetSession().SaveOrUpdate(dataObject);
}

My idea was to make a deepclone but sadly (for me) Nhibernate only updates the existing datarecord. I got some succes with

_transactionHelper.GetSession().Evict(dataObject);
_transactionHelper.GetSession().Save(dataObject.DeepClone());

But then Nhibernate Cascading features does not working properly and I get some times this exception detached entity passed to persist (what is correct).

Some Ideas? Or do i have to progamm is by myself :/

Thanks!

joh.scheuer
  • 566
  • 3
  • 11
  • I think the reason why NHibernate is just updating the existing datarecord is because the Primary key of the new object is set. Clearing the ID could help. Is it really a good idea to generate a deep copy? It sounds like a lot of redundant data!? – core Aug 22 '14 at 22:01
  • That's true. I try to remove the ID with the evict command and then assign a 0 ID. Now there a 2 Records in different versions which works pretty good. Sadly Fluent Nhibernate does not save all relations again. If an Element A has 1 relation to Element B and I create a new version of Element A then the database should contain two relations one for Element A version 1 and Element A version 2. The Database contains only 1 relation to the first version. I use DefaultCascade.SaveUpdate() Convention. Yes thats true and I removed it. – joh.scheuer Aug 25 '14 at 05:55

1 Answers1

0

I solved this problem by writing my own mapping container which tracks the state of a relation. I think the main problem was/is that I used my own composite tables (I needed to add some values like active).

To persist an allready persisted entity I used:

_transactionHelper.GetSession().Evict(dataObject);
dataObject.Id = 0;
_transactionHelper.GetSession().Save(dataObject);

It looks like this solution works very well for my problem.

joh.scheuer
  • 566
  • 3
  • 11