5

I would like to insert some data using plain sql into some tables that use hilo id generation in conjunction with nhibernate. Is this possible? I have found some similar questions but no definite answer yet. Thanks!

Chris

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • It *might* be possible, but it's really cumbersome. Why do you want to do that? – Diego Mijelshon Apr 21 '11 at 18:29
  • well my database has to be seeded with millions of rows using some automatic methods. but the db will be used via a web interface by users in the long run. I have now found that using stateless sessions seems to speed things up ... – cs0815 Apr 23 '11 at 06:51

1 Answers1

2

Sure you can do it. Just update the hi value in appropriate table and generate Id for inserts. Nhibernate won't validate ID's in DB.

unique key table is used only for inserts, and once object is in DB it doesn't matter anymore where its id came from

Sly
  • 15,046
  • 12
  • 60
  • 89
  • Can you please provide some more details? I have a table hibernate_unique_key which contains next_hi ... – cs0815 Nov 16 '11 at 10:51
  • beside nhibernate increases the id by 1000 for each new id but next_hi is a number below 1000 ... – cs0815 Nov 16 '11 at 10:52
  • For NHibernate when row is fetched from DB there is no difference between ID that was generated by hilo or by something else. You need just unique value there. – Sly Nov 16 '11 at 12:29
  • So let us say I have 30 tables. I have to determine all unique IDs from these tables than add 1 to the maximum (?) how do you update hibernate_unique_key so that nhibernate generates ids correctly next time it is used? – cs0815 Nov 16 '11 at 13:23
  • NHibernate will just read the value from unique key table, update it to become prev + 1 and that's it. So you need just to make sure that you are not using same hi value as NHibernate does. In order to achieve that you need to update hi value by yourself before usage – Sly Nov 16 '11 at 13:31
  • So what you are saying is read `hi` and add 1 and save back in hilo table. Then use this hi + 1 plus number of `lo's` for your own id's. I assume nhibernate always goes back to database to get its next hi which means this is safe(ish) – Rippo Dec 19 '14 at 15:51