0

I am using NHibernate to access a SQL Server database. I want to be able to encrypt the connection string. Reading StackOverflow and elsewhere, I found instructions to separate the connection string out to a connectionStrings section in the config file and reference the connection string by name in the nHibernate's connection_string_name field.

I have followed all of the instructions so far. If I have it configured with just the connection_string field, my queries all work successfully. If I separate it out, I get an exception when attempting to run a query.

The .config file contains these sections.

<configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate">
    </section>
</configSections>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.connection_string_name">nHibernateConnection</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    </session-factory>
</hibernate-configuration>

<connectionStrings>
    <add name="nHibernateConnection" connectionString="Data Source=(local);Password=Rapunz3l!;Persist Security Info=True;User ID=sa;Initial Catalog=VHT_Config" />
</connectionStrings>

The initialization code does this.

var config = new Configuration();
config.Configure();
sessionFactory = config.BuildSessionFactory();

Then the query code does this:

using (ISession session = sessionFactory.OpenSession())
{
    return session.CreateQuery("from myTable").List();
}

I get the following exception.

myTable is not mapped [from WSConfiguration]
at NHibernate.Hql.Ast.ANTLR.SessionFactoryHelperExtensions.RequireClassPersister(String name)
at NHibernate.Hql.Ast.ANTLR.Tree.FromElementFactory.AddFromElement()
at NHibernate.Hql.Ast.ANTLR.Tree.FromClause.AddFromElement(String path, IASTNode alias)
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.CreateFromElement(String path, IASTNode pathNode, IASTNode alias, IASTNode propertyFetch)
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElementList()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromClause()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.unionedQuery()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.query()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectStatement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.statement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(HqlParseEngine parser, String collectionRole)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 replacements, Boolean shallow, String collectionRole)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary`2 replacements, Boolean shallow)
at NHibernate.Engine.Query.HQLQueryPlan..ctor(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(String queryString, Boolean shallow, IDictionary`2 enabledFilters)
at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(String query, Boolean shallow)
at NHibernate.Impl.AbstractSessionImpl.CreateQuery(String queryString)
at VHT.Data.VHDAO.RetrieveAll(String tableClass) in C:\Projects\VH\Infrastructure\VHT.Data\VHDAO.cs:line 99
at MyWS.MyTableComponent.GetMyTableDataSet() 
in MyWS\MyDBComponent.cs:line 169

Any idea why this would fail only when the connection_string_name is configured? Am I missing something?

Thank you!

MarkP
  • 1
  • 1
  • this has nothing to do with connection string. myTable is no mapped class. Maybe you wanted `session.CreateSqlQuery("select * from myTable").List();` which is an Sql and no HQL query – Firo Jul 26 '13 at 07:27
  • That's interesting. I will try that. Odd that it would give no error with the other connection_string configuration. – MarkP Jul 26 '13 at 12:48
  • Unfortunately, changing that broke everything. I know it is strange, but the connection string is really the only thing that I changed. Why would it require the mapping in one and not in the other? – MarkP Jul 26 '13 at 13:05
  • does it help to specify the whole namespace of myTable? Is there any other error than the one above? is myTable really a class and mapped? – Firo Jul 26 '13 at 14:18
  • I get that same error on any table that I try to use. They are all mapped through corresponding .hbm.xml files (myTable.hbm.xml in this case) – MarkP Jul 26 '13 at 17:57
  • I tried fully qualifying and it still failed. – MarkP Jul 26 '13 at 18:31
  • Assuming you are using an xml mapping file. Have you set the mapping file of myTable as an embedded resource? – kagundajm Jul 27 '13 at 04:29

1 Answers1

0

CreateQuery takes a HQL string as parameter. When using HQL you are querying from the object and not the table. Therefore,

session.CreateQuery("from myTable").List();

will only work if the name of your class has a name myTable however if your class has a name MyTable, you will end up getting the error

myTable is not mapped 

So try changing the myTable to MyTable to conform with your class naming conventions.

kagundajm
  • 1,152
  • 1
  • 15
  • 26
  • I will try this, but that still doesn't seem like it would be an issue caused by changing my connectionString configuration. – MarkP Jul 31 '13 at 18:25