How do I get NHibernate to save an object if I assign an identifier to it, but generate it differently?

According to the REST philosophy, a request PUTmust update a resource at a URL if it exists, and create it if it does not exist. In other words, if I use the following URL:

PUT http://server/item/5

If exists Itemwith identifier 5, it will be updated. If it Itemdoes not exist with identifier 5, a new one Itemwith identifier 5 will be created .

However, I use NHibernate for persistence, and I have mapped my identifiers as Identity. This means that no matter what value I assign to the identifier, NHibernate will replace it with its own when I save the new one Item.

How do I get NHibernate to save Itemwith the identifier that I assigned to it, without changing the identifier mapping to Assigned?

+2
source share
1 answer

If you use Identity, DB will not allow you to enter a value.

However, if your database has a special syntax that allows you to insert explicit values ​​in the Identity fields, you can implement your own generator, which I guarantee will be error-prone, difficult to debug, and not very useful. But it is possible.

https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Id : -)

+2

Source: https://habr.com/ru/post/1771114/


All Articles