Tuesday, June 5, 2018

NHibernate auto-import

By default NHibernate supports the auto-import feature. This allows you to use unqualified class names in the HQL query language. This feature can become a problem if you have 2 classes with the same name (e.g. ExampleClass) but living in a different namespace(e.g. Example1Namespace,Example2Namespace).

If you try to initialize your SessionFactory, you will end up with the following error message:

"duplicate import:

Example refers to both

Example2Namespace.ExampleClass, Example2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null  and

Example1Namespace.ExampleClass, Example1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

(try using auto-import=\"false\")"

As a solution, you can set auto-import=false in your NHibernate mapping files.

Remark: This however does introduce another issue – it forces you to use the fully qualified name of these classes in HQL queries.

No comments: