2

Is there an easy way to make NH INSERT or UPDATE an entity depending on whether there is already an entity with same <natural-id />?

The entity is mapped to another (root) one using <many-to-one cascade="save-update" />.

Andrey Shchekin
  • 21,101
  • 19
  • 94
  • 162

1 Answers1

2

Not automatically. You will have to read from the database to see if a record for that natural-id already exists and then determine if you need to do an insert or update. Which of course means that the cascade won't work.

I was looking for something similar in How do I Insert or Update (or overwrite) a record using NHibernate?

Community
  • 1
  • 1
g .
  • 8,110
  • 5
  • 38
  • 48
  • 1
    Another solution is to write sql-insert in such way that it actually does upsert. But I would prefer a way to hook into NHibernate and force it to generate the correct insert. – Andrey Shchekin Nov 25 '09 at 13:02