15

I have my nHibernate setup and working correctly with QueryOver for most queries, however, whenever I try to do a HQL CreateQuery I get the exception that the entity isn't mapped. I can confirm that the same entity works fine using QueryOver.

Note: I am using fluent nHibernate

Any ideas what would cause this?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
johnnyboy
  • 869
  • 12
  • 23
  • 9
    Does it work if you use the fully-qualified name of the entity inside the HQL? – Andrew Whitaker Jul 05 '11 at 02:06
  • 6
    Yep just found this out myself. I'm using auto-import false in my conifg (as there are some entities, in different namespaces, with the same class name) and because of this in HQL the fully qualified name of the class must be used. Hope this helps someone else too :) – johnnyboy Jul 05 '11 at 02:17
  • 7
    You might want to post this as an answer. – Sandor Drieënhuizen Jun 13 '12 at 10:21
  • @AndrewWhitaker ditto with SandorDrieënhuizen – JM Hicks Feb 27 '14 at 13:37
  • It wasn't obvious to me when trying to fully-qualify the name, that I had to add the name of the solution as well, like [vs solution name].[project name].[folder level1]....[folder level-n].[entity class name] to get it to work. – JM Hicks Feb 27 '14 at 14:19

1 Answers1

7

If you have disabled auto-import in your mappings (<hibernate-mapping auto-import="false">), then you will have to use fully-qualified class names everywhere in your queries, unqualified class names won't work.

Otherwise, enable auto-import.

Conventions.Setup(x =>
                     {
                         x.Add(FluentNHibernate.Conventions.Helpers.AutoImport.Always()); // AutoImport.Never
                     }); // End FluentMappings.Conventions.Setup

Like this:

/*
var model = AutoMap.AssemblyOf<MyDb>()
                .Where(t => t.Namespace.StartsWith("MyDb.Tables"))
                .Conventions.AddFromAssemblyOf<MyDb>();
*/




        protected static AutoPersistenceModel CreateMappings()
        {
            //return new AutoPersistenceModel().AddMappingsFromAssemblyOf<MyDB.Tables.T_Admin>();

            return new AutoPersistenceModel().AddMappingsFromAssemblyOf<MyDb.Tables.T_Admin>()
                 .Where(t => t.Namespace == "MyDb.Tables");
        }

    private static ISessionFactory CreateMsSqlSessionFactory()
    {
        //AutoPersistenceModel model = CreateAutoMappings();
        AutoPersistenceModel model = CreateMappings();

        return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2005
            .ConnectionString(c => c
                //.Server("MYCOMPUTER\\SQLEXPRESS")
                .Server("localhost")
                //.Database("testdb")
                .Database("nhDMS")
                .Username("TableCreatorWebServices")
                .Password(DB.Tools.Cryptography.AES.DeCrypt("AES_ENCRYPTED_PW"))))
            //.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SsoToken>()) 
            .Mappings(m => 
                {
                    m.AutoMappings.Add(model);
                    m.FluentMappings.Conventions.Setup(x =>
                     {
                         //x.AddFromAssemblyOf<MyDb.Tables.T_Admin>();
                         x.Add(FluentNHibernate.Conventions.Helpers.AutoImport.Always()); // AutoImport.Never
                     }); // End FluentMappings.Conventions.Setup
                }

            ) // End Mappings
            .ExposeConfiguration(BuildSchema)  // BuildSchema function call...
            .BuildSessionFactory();
    } // End Function CreateMsSqlSessionFactory
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442