According to REST philosophy, a PUT
operation should (taken from Wikipedia):
PUT http://example.com/resources/142
Updat开发者_C百科e the address member of the collection, or if it doesn't exist, create it.
NHibernate seems to have two ways of dealing with entity IDs:
- Auto-generate an ID, regardless of what value the user set.
- Use the ID assigned by the user, but lose all auto-generation capabilities.
The problem here with a PUT
operation is the part about creating the entity if it doesn't exist. My assumption is that if you PUT
a resource that doesn't exist, it will create it with the same ID as specified by the URL (such as 142 if we use the above example). However, NHibernate doesn't allow you to set the ID if it's auto-generated.
So my question is, is there a way to get NHibernate to auto-generate an ID if the entity doesn't have one (or has the default value for the ID type, for example 0 for ints), but also save the entity with the ID that the user set?
Generally its a bad idea to use assigned ids.
The situation that you have is closer to the thing called NaturalId. You should use it I think. You will need to have two different properties, one for databases primary key, and second as a id that is visible to users.
精彩评论