I try to start using NHibernate in my apps and found a thing, that is looking strange for me:
I have a class Phonemoney with mapping to table phonemoney. I have the property named 'UnlockKey', which is mapped to 'unlock_key' column. When I try to use
session.Find("from Phonemoney where UnlockKey=?",....), I recieved an error:
Unknown column 'Phonemoney.UnlockKey' in 'where clause'
If I change query to "from Phonemoney where unlock_key=?", Find function works fine.
I thought, that I could forget about internal database implementation and using only business objects and its properties in my queries... What am I doing wrong or I must use column names in HQL?
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 1.2.1.4000
Mapping documents:
Code:
<class name="MyNamespace.Phonemoney,NHibernateTypesMappings" table="phonemoney" lazy="true">
<id name="Id" column="id" type="int">
<generator class="native" />
</id>
<property name="TransactionId" column="transaction_id" type="long" not-null="true" />
<property name="RequestTime" column="request_time" type="DateTime" not-null="true" />
<property name="Keyword" column="keyword" type="string" />
<property name="Provider" column="provider" type="string" />
<property name="ShortNumber" column="short_number" type="string" />
<property name="Phone" column="phone" type="string" />
<property name="RequestText" column="request_text" type="string" />
<property name="RequestFulltext" column="request_fulltext" type="string" />
<property name="Cost" column="cost" type="Decimal" not-null="true" />
<property name="UnlockKey" column="unlock_key" type="string" />
<property name="UnlockKeyUsed" column="unlock_key_used" type="byte" not-null="true" />
</class>
Code between sessionFactory.openSession() and session.close():Code:
ISession hibernateSession = NHibernateHelper.GetCurrentSession();
ITransaction tx = hibernateSession.BeginTransaction();
IList m=hibernateSession.Find("from Phonemoney where UnlockKey=? and UnlockKeyUsed=0", code, NHibernateUtil.Int32);
tx.Commit();
NHibernateHelper.CloseSession();
Full stack trace of any exception that occurs:
2008-01-14 14:56:05,359 [5] INFO (NHibernate.Loader.Loader) - select phonemoney0_.id as id0_, phonemoney0_.transaction_id as transact2_0_, phonemoney0_.request_time as request3_0_, phonemoney0_.keyword as keyword0_, phonemoney0_.provider as provider0_, phonemoney0_.short_number as short6_0_, phonemoney0_.phone as phone0_, phonemoney0_.request_text as request8_0_, phonemoney0_.request_fulltext as request9_0_, phonemoney0_.cost as cost0_, phonemoney0_.unlock_key as unlock11_0_, phonemoney0_.unlock_key_used as unlock12_0_ from phonemoney phonemoney0_ where (UnlockKey=?p0 )and(UnlockKeyUsed=0 )
2008-01-14 14:56:05,515 [5] WARN (NHibernate.Util.ADOExceptionReporter) - MySql.Data.MySqlClient.MySqlException: Unknown column 'UnlockKey' in 'where clause'
at MySql.Data.MySqlClient.MySqlStream.OpenPacket()
at MySql.Data.MySqlClient.NativeDriver.ReadResult(UInt64& affectedRows, Int64& lastInsertId)
at MySql.Data.MySqlClient.MySqlDataReader.GetResultSet()
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
at NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd)
at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, RowSelection selection, ISessionImplementor session)
2008-01-14 14:56:05,609 [5] ERROR (NHibernate.Util.ADOExceptionReporter) - Unknown column 'UnlockKey' in 'where clause'
Name and version of the database you are using:
MySql 5.1
The generated SQL (show_sql=true):
select phonemoney0_.id as id0_, phonemoney0_.transaction_id as transact2_0_, phonemoney0_.request_time as request3_0_, phonemoney0_.keyword as keyword0_, phonemoney0_.provider as provider0_, phonemoney0_.short_number as short6_0_, phonemoney0_.phone as phone0_, phonemoney0_.request_text as request8_0_, phonemoney0_.request_fulltext as request9_0_, phonemoney0_.cost as cost0_, phonemoney0_.unlock_key as unlock11_0_, phonemoney0_.unlock_key_used as unlock12_0_ from phonemoney phonemoney0_ where (UnlockKey=?p0 )and(UnlockKeyUsed=0 )
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html