开发者

NHibernate - how to switch dynamically the generator for Id?

开发者 https://www.devze.com 2023-02-12 15:52 出处:网络
Right now I have all my mappings as hb开发者_如何学JAVAm.xml. I want to switch dynamically the type of Id generator for certain entities from \'identity\' to \'assigned\' at runtime (application start

Right now I have all my mappings as hb开发者_如何学JAVAm.xml. I want to switch dynamically the type of Id generator for certain entities from 'identity' to 'assigned' at runtime (application start).

This is because I need to support importing data from previous system and keep existing ids.

Is this possible? How?


The generator is part of the mappings, so you need to change the mappings before creating the session factory.

This is easy to do with Fluent or ConfORM. It's possible to change XML mappings before feeding them to the configuration, but it's cumbersome.

Just check for a configuration flag (that you'll change when starting the app), and call the appropriate generator.


It's not clear why you would need to keep existing id's. I think you should not be needing to keep existing id's. Maybe you need to keep alternate id's instead?

If the previous system has it's own database, then you: 1) Need another mapping for the other table in the other database 2) Copy the data to your existing database (with key identity)

Which means you will need new id's anyway. Example: Suppose you want to copy a table of 'airlines' and the previous system uses the 'airline-code' as the primary key. You could use an integer as primary key in your new database and the airlinecode as your alternate key.

0

精彩评论

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