Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nHibernate HQL - entity is not mapped

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?

like image 506
johnnyboy Avatar asked Jul 05 '11 02:07

johnnyboy


1 Answers

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
like image 54
Stefan Steiger Avatar answered Oct 07 '22 15:10

Stefan Steiger