-->
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.  [ 5 posts ] 
Author Message
 Post subject: after migration to hib3 HQL containging " doesn't work
PostPosted: Thu Sep 01, 2005 8:48 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:03 pm
Posts: 40
String queryString = "from " + XY.class.getName() + " WHERE stringTypeAtribute = \"" + param1 + "\"";
getHibernateTemplate().find(queryString);

end with this exception. On hibernate 2 works fine. why? are there any similiar incompatibilities betwen hib2 and hib3?

org.springframework.orm.hibernate3.HibernateQueryException: unexpected char: '"' [select count(*) from cz.aura.isl.katalog.davky.domain.transaction.AdresatTransakce as adresat WHERE adresat.pk.kmoeext = "dasfdsf" AND adresat.pk.outputHeader.id = 43]; nested exception is org.hibernate.QueryException: unexpected char: '"' [select count(*) from cz.aura.isl.katalog.davky.domain.transaction.AdresatTransakce as adresat WHERE adresat.pk.kmoeext = "dasfdsf" AND adresat.pk.outputHeader.id = 43] org.hibernate.QueryException: unexpected char: '"' [select count(*) from cz.aura.isl.katalog.davky.domain.transaction.AdresatTransakce as adresat WHERE adresat.pk.kmoeext = "dasfdsf" AND adresat.pk.outputHeader.id = 43] at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:165) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427) at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884) at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:920) at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:41) at cz.aura.isl.katalog.dao.impl.OutputTransactionDAOImpl$1.doInHibernate(OutputTransactionDAOImpl.java:178) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:315) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:288) at cz.aura.isl.katalog.dao.impl.OutputTransactionDAOImpl.countFromKadrtra(OutputTransactionDAOImpl.java:183) at cz.aura.isl.katalog.dao.impl.OutputTransactionDAOImplTest.testcountFromKadrtra(OutputTransactionDAOImplTest.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


Top
 Profile  
 
 Post subject: hmm
PostPosted: Sat Sep 03, 2005 12:27 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I did not even know we could use double quote in the query like that :)

There are few reasons why HQL queries should not be constructed like that:
1. Standard SQL symbol for quoting literals is single quote;
2. If parameter param1 will have single or double quot in it, then then the query will fail at runtime.

There are convenience functions in Hibernate to avoid the stated above problems:
Query query = session.createQuery("from A1 a where a.someProp = :p");
query.setString( "p", param1);
Object o = query.uniqueResult();

Or with position parameters:
Query query = session.createQuery("from A1 a where a.someProp = ?");
query.setString( 0, param1);
Object o = query.uniqueResult();

Or as oneliner
Object o = session.createQuery("from A1 a where a.someProp = ?").setString( 0, param1 ). uniqueResult();

With this approach the value of ‘param1’ will be properly escaped and quoted .

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 11:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is not true that that query has ever worked in Hibernate. The quote character in HQL and SQL is ', not ".


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 11:43 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:03 pm
Posts: 40
gavin wrote:
It is not true that that query has ever worked in Hibernate. The quote character in HQL and SQL is ', not ".


that query has worked in hibernate. the database columnt type is informix's char, but in xml is mapped as string, and on hibernate 2 has worked.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 4:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So, informix is unusual in that it supports double-quoted string literals. This is contrary to ANSI SQL where double quotes are used for quoting identifiers.

You should not write queries that use " to quote string literals, since they are not (and have never been) valid HQL and will not work on other databases. For a definition of valid HQL syntax, refer to the reference documentation and HiA.


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