How to create an object via RESTful PUT with NHibernate?

According to the REST philosophy, a PUT operation must create an entity if it does not exist, or update if this happens. For example, if a client performs this operation:

PUT http://server/user/5?firstname=John&lastname=Doe

I should expect user with id 5 to be created or updated.

The upgrade case is simple in NHibernate; just upload the user and update the first and last name.

However, how do I create a user with identifier 5? By default, NHibernate manages all entity identifiers. Even if you set the identifier yourself, NHibernate will ignore it and replace it with its own. If I switch to using the assigned identifiers, I can assign a new user with identifier 5, but then I will lose many NHibernate functions.

In other words, is there a way to configure NHibernate to use the generated identifier if it is not provided, and to use the user identifier if it is provided? If not, how do I get around this problem of creating a PUT using NHibernate?

+3
source share
1 answer

If you follow

PUT http://server/user/5

and the server creates the object, but nHibernate changes the identifier, then returns an HTTP status code 301 - Moved permanentlyand puts the new URI in the Location header.

The client must discover 301 and update all stored URLs with the old identifier.

Just a word of warning, the semantics of PUT do not actually create or update, they replace. Therefore, if you send a submission containing only the last name and first name, then if you do something according to the book, then all other information that was previously stored by this user is lost.

0
source

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


All Articles