I have been using the generator class assigned
to assign my primary key values. I have read several blogs which state that hilo
is better. Is there a benefit to assigned
over hilo
?
-
What is better depends on how you use assigned. What do you assign? – Paco Aug 09 '10 at 20:46
1 Answers
assigned
is just useful when you want to define the primary key yourself.
If you get the value from somewhere, for instance from another database or an imported file, assigned
could be useful. But then it's most certainly a guid, not an integer. You may also use natural primary keys, which are business data and primary key at the same time. Although it is discouraged to do this.
I would say, assigned
is for special cases, not for regular applications. Usually you use artificial keys, so you don't care about the exact value. Generating the value yourself could be difficult, at least when you consider several processes using the same database.
There is a major difference between assigned
and (most?) other id generators. assigned
keys are not given by NHibernate nor by the database. NHibernate usually knows if an entity is already in the database by evaluating the id. When you assign it yourself, you also need to know yourself if the entity needs to be inserted or updated. This make this id generator special. Your code will look different when you use assigned
as when you use any other id generator.

- 63,782
- 15
- 129
- 193
-
so basically I can modify my hbm.xml files to use the generator `hilo` and remove instances of setting the primary key in my entities? – Aug 09 '10 at 20:29
-
1Yes, when you use `hilo` (or any other generator except `assigned`), you don't need to generate the primary key yourself. The primary key is set at the moment when the entity is put into the session (eg. when calling Update, Merge, SaveOrUpdate). Hilo does (most of the time) not need a database roundtrip to generate the id. (If you set the hilo-generated id yourself, you will experience strange behaviour ...) – Stefan Steinegger Aug 10 '10 at 08:45