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.  [ 8 posts ] 
Author Message
 Post subject: HQLs Vs Session.Find
PostPosted: Fri Nov 03, 2006 12:44 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear members,

i'm using the following queries , they are very simple queries but i can't get them to work :(, and exceptions are thrown .

Query 1
string hql = "SELECT Cat.CatItems FROM Business.Category as Cat WHERE Cat.Id=" + CatID;
session.find(hql) ...

Exception

Quote:
Incorrect query syntax [SELECT Cat.idcategoryiditems FROM nhExhibition.Business.Category as Cat WHERE Cat.Id=5]
Parameter name: DBHandler - ERROR IN Getting Department Properties
Actual value was NHibernate.QueryException: Incorrect query syntax [SELECT Cat.idcategoryiditems FROM nhExhibition.Business.Category as Cat WHERE Cat.Id=5] ---> System.ArgumentException: Item has already been added. Key in dictionary: 'Cat' Key being added: 'Cat'
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at NHibernate.Hql.QueryTranslator.SetAliasName(String alias, String name)
at NHibernate.Hql.FromParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ClauseParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.PreprocessingParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.QueryTranslator.Compile()
--- End of inner exception stack trace ---
at NHibernate.Hql.QueryTranslator.Compile()
at NHibernate.Hql.QueryTranslator.Compile(ISessionFactoryImplementor factory, IDictionary replacements, Boolean scalar)
at NHibernate.Impl.SessionFactoryImpl.GetQuery(String queryString, Boolean shallow)
at NHibernate.Impl.SessionImpl.GetQueries(String query, Boolean scalar)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.List()
at nhExhibition.DataAccess.ExhibitionDBMgr.SELECT_PROPERTIES(String hql) in C:\Documents and Settings\Jawad\Desktop\Monday 30 October 2006\Exhibition - DataTutorial\Exhibition\project\Exhibition 03\nhExhibition.DataAccess\ExhibitionDBMgr.cs:line 701.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Incorrect query syntax [SELECT Cat.idcategoryiditems FROM nhExhibition.Business.Category as Cat WHERE Cat.Id=5]
Parameter name: DBHandler - ERROR IN Getting Department Properties
Actual value was NHibernate.QueryException: Incorrect query syntax [SELECT Cat.idcategoryiditems FROM nhExhibition.Business.Category as Cat WHERE Cat.Id=5] ---> System.ArgumentException: Item has already been added. Key in dictionary: 'Cat' Key being added: 'Cat'
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at NHibernate.Hql.QueryTranslator.SetAliasName(String alias, String name)
at NHibernate.Hql.FromParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ClauseParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.PreprocessingParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.QueryTranslator.Compile()
--- End of inner exception stack trace ---
at NHibernate.Hql.QueryTranslator.Compile()
at NHibernate.Hql.QueryTranslator.Compile(ISessionFactoryImplementor factory, IDictionary replacements, Boolean scalar)
at NHibernate.Impl.SessionFactoryImpl.GetQuery(String queryString, Boolean shallow)
at NHibernate.Impl.SessionImpl.GetQueries(String query, Boolean scalar)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.List()
at nhExhibition.DataAccess.ExhibitionDBMgr.SELECT_PROPERTIES(String hql) in C:\Documents and Settings\Jawad\Desktop\Monday 30 October 2006\Exhibition - DataTutorial\Exhibition\project\Exhibition 03\nhExhibition.DataAccess\ExhibitionDBMgr.cs:line 701.


What if i want to get Cat.CatItems.Id, Cat.CatItems.Name ... using the same query ?

Query 2
string hql = "SELECT Cat.idcategoryiditems FROM nhExhibition.Business.Category as Cat WHERE Cat.Id=" + CatID;

int retrievedNumber = 0;
object myInstance=null;
myInstance = m_session.CreateQuery(hql).UniqueResult();
tx.Commit();
if (myInstance != null)
retrievedNumber = Convert.ToInt16(myInstance);

Exception
Quote:
Incorrect query syntax [SELECT myCat.cathasparent.Id FROM nhExhibition.Business.Category as myCat WHERE myCat.Cat='Electronic Devices' or upper('Electronic Devices') or lower('Electronic Devices')]
Parameter name: DBHandler - ERROR IN Checking Category Name
Actual value was NHibernate.QueryException: Incorrect query syntax [SELECT myCat.cathasparent.Id FROM nhExhibition.Business.Category as myCat WHERE myCat.Cat='Electronic Devices' or upper('Electronic Devices') or lower('Electronic Devices')] ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at NHibernate.Hql.WhereParser.CloseExpression(QueryTranslator q, String lcToken)
at NHibernate.Hql.WhereParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ClauseParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.PreprocessingParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.QueryTranslator.Compile()
--- End of inner exception stack trace ---
at NHibernate.Hql.QueryTranslator.Compile()
at NHibernate.Hql.QueryTranslator.Compile(ISessionFactoryImplementor factory, IDictionary replacements, Boolean scalar)
at NHibernate.Impl.SessionFactoryImpl.GetQuery(String queryString, Boolean shallow)
at NHibernate.Impl.SessionImpl.GetQueries(String query, Boolean scalar)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.List()
at NHibernate.Impl.AbstractQueryImpl.UniqueResult()
at nhExhibition.DataAccess.ExhibitionDBMgr.SELECT_CHECK(String hql) in C:\Documents and Settings\Jawad\Desktop\Monday 30 October 2006\Exhibition - DataTutorial\Exhibition\project\Exhibition 03\nhExhibition.DataAccess\ExhibitionDBMgr.cs:line 718.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Incorrect query syntax [SELECT myCat.cathasparent.Id FROM nhExhibition.Business.Category as myCat WHERE myCat.Cat='Electronic Devices' or upper('Electronic Devices') or lower('Electronic Devices')]
Parameter name: DBHandler - ERROR IN Checking Category Name
Actual value was NHibernate.QueryException: Incorrect query syntax [SELECT myCat.cathasparent.Id FROM nhExhibition.Business.Category as myCat WHERE myCat.Cat='Electronic Devices' or upper('Electronic Devices') or lower('Electronic Devices')] ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at NHibernate.Hql.WhereParser.CloseExpression(QueryTranslator q, String lcToken)
at NHibernate.Hql.WhereParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ClauseParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.PreprocessingParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.QueryTranslator.Compile()
--- End of inner exception stack trace ---
at NHibernate.Hql.QueryTranslator.Compile()
at NHibernate.Hql.QueryTranslator.Compile(ISessionFactoryImplementor factory, IDictionary replacements, Boolean scalar)
at NHibernate.Impl.SessionFactoryImpl.GetQuery(String queryString, Boolean shallow)
at NHibernate.Impl.SessionImpl.GetQueries(String query, Boolean scalar)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.List()
at NHibernate.Impl.AbstractQueryImpl.UniqueResult()
at nhExhibition.DataAccess.ExhibitionDBMgr.SELECT_CHECK(String hql) in C:\Documents and Settings\Jawad\Desktop\Monday 30 October 2006\Exhibition - DataTutorial\Exhibition\project\Exhibition 03\nhExhibition.DataAccess\ExhibitionDBMgr.cs:line 718.



My purpose in Query 2 is to get the CategoryId of a specific Category(based on its Name), i'm getting one Category Id if the Cat exists .. that's why i used m_session.CreateQuery(hql).UniqueResult();

By the way, does anyone know how can i retrieve the last id inserted in a class(table) using a separate query ?

Regards,
JojoRico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 04, 2006 9:04 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Hello .... Anybody there ?

it's really urgent !
so if anyone have suggestions,i would be glad to hear it

regards

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 04, 2006 9:50 am 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Query 1:
string hql = "from CatItems ci where ci.Category.Id = ?"
session.find(hql, catId)

Query 2:
string hql = "select ci.Id, ci.Name from CatItems ci where ci.Category.Id = ?"
session.find(hql, catId)


Top
 Profile  
 
 Post subject: Session Find
PostPosted: Sun Nov 05, 2006 2:20 pm 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear Ayende Rahien

thanks for your reply ..
i have a few other questions, but i'll test your queries before that and group all the questions in one.

Regards,
JojoRico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: Third Parameter
PostPosted: Sun Nov 05, 2006 3:45 pm 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear Ayande

there is a third parameter in the
Session.find(par1,par2,par3) where par3 is of NHibernate.Type.DateTimeType type, what should it be in the case of DateTime value (example if possible)

thanks in advance,
regards,
Jojorico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: Session.Find Example !
PostPosted: Sun Nov 05, 2006 5:27 pm 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear Ayende, Nhibernate users

here is a simple example to clarify my idea :

Ex:
let's suppose we have a Collection of Items which contains a Collection of Keywords, i want to :
get the list of keywords having Dateofpublish < DateTime.now and which are not for this item ... so i have 2 criterias in my case :
1) Get the keywords having Date <Datetime.now
2) which are not for a particular item

My question is how to handle :
DateTime format using session.find(hql,datecriteria, parameter 3 ) ?
what is parameter 3 ?
How to specify that these keywords must not include keywords for a specific item ?
is it better to use criteria or hql in this case (performance and easy usability)?
can you please provide an example to clarify the solution ?

Thanks for your time,

Regards,
JojoRico

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 6:45 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
it's NHibernate.HibernateUtil.DateTime
i'm still working on the other issues

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 1:55 pm 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
hey guys, is there anybody here ?

_________________
In Code We Trust


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.