views:

210

answers:

3

I'm using NHibernate to connect to an ERP database on our DB2 server. We have a test schema and a production schema. Both schemas have the same table structure underneath. For testing, I would like to use the same mapping classes but point NHibernate to the test environment when needed and then back when in production. Please keep in mind that we have many production schemas and each production schema has an equivalent test schema.

I know that my XML mapping file has a schema property inside it, but since it's in XML, it's not like I can change it via a compiler directive or change the schema property based on a config file.

Any ideas?

Thank You.

+1  A: 

No need to specify schema in the mappings: there's a SessionFactory-level setting called default_schema. However, you can't change it at runtime, as NHibernate pregenerates and/or caches SQL queries, including the schema part.

zvolkov
exactly, so the solution is: build another session factory.
Mauricio Scheffer
ahh, good point Mauricio
zvolkov
Thanks for the reply. Based on your info, I don't think I have a choice but to keep the schema info inside the mapping file and change it when I'm done with testing. I have at least two production schemas so setting a default schema in the factory would require a factory for every schema, or having the schema property set for some mappings but not others. I've read that factories are heavy and to avoid more than one so I think I'm left with changing just the schema properties in the mapping files. I wish I could create a mapping interface and override just the property instead. Thanks Again.
Brian
Factories are expensive to create at run time, but not so expensive to keep around. I don't understand how you plan on changing mappings at run time (as alternative to just using the other factory)
zvolkov
I was giving up on the idea of changing the schemas at run-time and instead thought that I would just declare the schema in the mapping file. Is that feasible? Per your suggestion, if I have two different factories based on two different schemas, can I still do a join between two tables from the different schemas or have a master/detail relationship?
Brian
nah, can't do cross-schema queries at runtime unless those entities belong to the same factory and schema is specified in the mappings like you say.
zvolkov
A: 

Take a look at SchemaUpdate.

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/28/create-and-update-database-schema.aspx

zowens
err I suppose that's not exactly what he meant :)
zvolkov
could have helped, ya never know :)
zowens
A: 

To get what I wanted, I had to use NHibernate.Mapping.Attributes.

[NHibernate.Mapping.Attributes.Class(0, Table = “MyTable”, Schema = MySchemaConfiguration.MySchema)]

In this way, I can create a class like MySchemaConfiguration and have a property inside of it like MySchema. I can either set the property's value via a compiler directive or get it through a configuration file. This way I only have to change the schema in one place and it will be reflected throughout all of the other mappings.

Brian