开发者

Is it possible to get NHibernate to auto-generate an ID if the entity does not have one, or use the ID of the entity if it already has one?

开发者 https://www.devze.com 2023-01-20 12:42 出处:网络
According to REST philosophy, a PUT operation should (taken from Wikipedia): PUT http://example.com/resources/142

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:

  1. Auto-generate an ID, regardless of what value the user set.
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消