I would say, that in this scenario your custom approach is simply correct, despite of your note:
Currently I have to check this manually, but I want to avoid this because it's a lot of extra effort
The issue, why it could be hardly managed by NHiberante, is hidden in functionality NHibernate is offering to us, when working with references and their Id
s (I.e: Bustype
and Bustype.Id
)
- either they do have
Id
a value == NHibernate expects such an object does exist, no need to INSERT - the 'Id' is exactly equal to commonly expected "
unsaved-value
". We can configure, what is unsaved-value (null, -1, 0...) but we can have only ONE value representing the transient == unsaved instance.
Regardless of the fact that your question is about INSERT, this documeneted section (please, if possible, read it carefully) would give more understanding:
Summary:
If objects coming from client (e.g. Excel sheet) do have the Id
pre-set, regardless of the fact they are persisted or not, NHibernate needs our help ...