NHibernate Override Configuration - NHibernate

What is NHibernate Override Configuration?

In this section, we will be casing how to override NHibernate configuration. There are just a few things you need to keep in mind.

  • Mainly, configuration in NHibernate is additive.
  • So you don't just have to use a single xml file or you don't just have to use the code-based configuration or fluent NHibernate.
  • You can mix and match all of these methods depending on how you want to configure your application.
  • The significant point to recall is that, finally configuration wins.

In the next instance, you can see that we make our configuration object, configure it using the code-based configuration and lastly call the cfg.configure() method, which loads the hibernate.cfg.xml file.

  • So whatever inside of a hibernate.cfg.xml dominates the settings set by code-based configuration?
  • By retreating these two procedures we can have the defaults inside of hibernate.cfg.xml and then do our overrides inside of a code-based configuration.
  • There is nobody that rejects if you are using code-based configuration and also there is nothing that prevents you from using the hibernate.cfg.xml file.
  • Let’s have a look into a easy instance in which we will override the configuration by using a mixture of xml-based and code-based configuration.

Let’s also move the connection string to the app.config file using the following code.

The connection string is sitting in some app.config file with a default name. Now, we need to mention the default name in hibernate.cfg.xml file instead of the connection string.

Let’s comment on the connection string part, driver, and dialect part from the code-based configuration, because the program will read it from the hibernate.cfg.xml file and the LogSqlInConsole part will remain in the code-based configuration.

Currently when you run the application, you will realize that the program has read the log from code-based configuration and other configuration from the hibernate.cfg.xml file.

So at present we have got some of our configuration inside of our hibernate.cfg.xml file, certain of it is inside of the code-based configuration and depending on the order of calling code-based versus configure(), we can change which of them overrides the other.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

NHibernate Topics