nHibernate HQL - entity is not mapped
Asked Answered
A

1

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?

Awash answered 5/7, 2011 at 2:5 Comment(5)
Does it work if you use the fully-qualified name of the entity inside the HQL?Furlani
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 :)Awash
You might want to post this as an answer.Agreed
@AndrewWhitaker ditto with SandorDrieënhuizenShithead
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.Shithead
I
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
Impossible answered 13/8, 2012 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.