-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Basic question about queries
PostPosted: Tue Jan 15, 2008 8:55 pm 
Newbie

Joined: Tue Jan 15, 2008 6:57 pm
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 8:01 am 
Beginner
Beginner

Joined: Tue Mar 27, 2007 4:54 am
Posts: 47
Try...
"from Phonemoney pm where pm.UnlockKey=?"
...instead

I would also recommend that you skip the obsolute Find() method and instead have a look at IQuery or ICriteria interfaces (CreateQuery()/CreateCriteria()).

/Roger


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.