This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
Having a private setter on an entity’s id property may seem tempting: in most cases, unless you are using id generators assigned or foreign, you never have to set its value directly.
However, keep this in mind:
- If your entity is lazy and you want to prevent people from setting its value, make the setter protected instead of private, because it will need to be accessed from subclasses of your entity (generated by NHibernate);
- If you use stateless sessions, you can perform some operations which, on regular sessions, require you to load an entity, without doing so, for example: