I have a parsing error and i would like to know if anybody had already this problem. Libraries used for hibernate : hibernate-core 3.3.2.GA hibernate-annotations 3.4.0.GA net.sf.ehcache 1.6.2 slf4j-log4j12 1.5.10 javassist 3.9.0.GA

aquahellishSoftware and s/w Development

Dec 13, 2013 (3 years and 3 months ago)

54 views

I have a parsing error and i would like to know if anybody had already this problem.


Libraries used for hibernate :

hibernate
-
core 3.3.2.GA

hibernate
-
annotations 3.4.0.GA

net.sf.ehcache 1.6.2

slf4j
-
log4j12 1.5.10

javassist 3.9.0.GA

cglib 2.2

antlr 2.
7.6

dom4j 1.6.1


Entity « Courriel »:


Code:

@Entity

@Table(name = "COURRIEL")

public class Courriel implements java.io.Serializable {





private static final long serialVersionUID = 1L;




private int oid;




private String adresse;





@SequenceGenera
tor(name = "generator", sequenceName = "myseq_courriel")




@Id




@GeneratedValue(strategy = SEQUENCE, generator = "generator")




@Column(name = "OID", nullable = false)




public int getOid() { return this.oid; }




public void setOid(int oid) { this.oi
d = oid; }





@Column(name = "ADRESSE")




public String getAdresse() { return this.adresse; }




public void setAdresse(String adresse) { this.adresse = adresse; }

}


SpringContext :

Code:

<bean id="mySessionFactory"
class="org.springframework.orm.hibern
ate3.annotation.AnnotationSessionFactoryBean">






<property name="annotatedClasses">






<list>








<value>gouv.education.ramsese.uai.Courriel</value>






</list>






</property>






<property name="hibernateProperties">








<props>










<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>










<prop
key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>













</props>






</property>






<property na
me="dataSource" ref="dataSource"/> <!
--

retrieve by JNDI / or BasicDataSource
--
>




</bean>





<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">






<property name="sessionFactory" ref="mySessionFactory"/>




</b
ean>




Unit Test via AbstractDependencyInjectionSpringContextTests :

Code:

// SpringContext Loaded by AbstractDependencyInjectionSpringContextTests in class HibernateUnitTest

public class HibernateTest extends HibernateUnitTest {







private HibernateT
emplate hibernateTemplate;




public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {






this.hibernateTemplate = hibernateTemplate;





}





@SuppressWarnings("unchecked")




public void testHQLCast() {






StringBuilder hqlQuery = n
ew StringBuilder("select cast(c.oid as varchar(100)) from Courriel c");






Session session = hibernateTemplate.getSessionFactory().getCurrentSession();






Query query = session.createQuery(hqlQuery.toString());






List<String> list = query.list();











assertFalse(list.isEmpty());









}




}




Configuration 1:


Dialect:

org.hibernate.dialect.DB2Dialect

QueryTranslatorFactory:

org.hibernate.hql.classic.ClassicQueryTranslatorFactory (Old Factory)


Parsing error:

A concatenate is realized

at HQL : cast(c.oid as varchar(100))
-
> SQL : l0_.OIDasvarchar(100

Code:

org.hibernate.exception.SQLGrammarException: could not execute query




at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)




at org.hibernate.exception.
JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)




at org.hibernate.loader.Loader.doList(Loader.java:2235)




at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)




at org.hibernate.loader.Loader.list(Loader.java:2124)




at org.
hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:935)




at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)




at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)




at org.hibernate.impl.QueryImp
l.list(QueryImpl.java:102)




at gouv.education.ramsese.test.HibernateTest.testHQLCast(HibernateTest.java:106)

Caused by: com.ibm.db2.jcc.a.ho: DB2 SQL Error: SQLCODE=
-
104, SQLSTATE=42601,
SQLERRMC=);l0_.OIDasvarchar(100;<space>, DRIVER=4.3.85

Configurati
on 2

:


Dialect
: org.hibernate.dialect.DB2Dialect

QueryTranslatorFactory
: org.hibernate.hql.ast.ASTQueryTranslatorFactory (Default
Factory in hibernate
3.3.2.GA)


Parsing error
:

The cast function is not parsing correctly :

Code:

org.hibernate.hql.ast.Query
SyntaxException: expecting CLOSE, found '(' near line 1, column 29 [select
cast(c.oid as varchar(100)) from gouv.education.ramsese.uai.Courriel c]




at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)




at org.hibernate.hq
l.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)




at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)




at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)




at org.hibernate.hql.
ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)




at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)




at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)




at org.hibernate.engine.que
ry.HQLQueryPlan.<init>(HQLQueryPlan.java:80)




at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)




at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)




at org.hibernate.impl.Abstrac
tSessionImpl.createQuery(AbstractSessionImpl.java:135)




at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)




at gouv.education.ramsese.test.HibernateTest.testHQLCast(HibernateTest.java:105)

)

Configuration 1

with where clause
:


Dialect
:

org.hibernate.dialect.DB2Dialect

QueryTranslatorFactory:

org.hibernate.hql.classic.ClassicQueryTranslatorFactory (Old Factory)

.hibernate.hql.classic.ClassicQueryTranslatorFactory (Old Factory)

Code:




@SuppressWarnings("unchecked")




public void testH
QLCastWhere() {






StringBuilder hqlQuery = new StringBuilder("select c.libelle from Courriel c where c.adresse =


cast(?
as varchar(100))");






Session session = hibernateTemplate.getSessionFactory().getCurrentSession();






Query query = session.
createQuery(hqlQuery.toString());






query.setString(0, "test@test.com");






List<String> list = query.list();











assertFalse(list.isEmpty());









}

Or

Code:




@SuppressWarnings("unchecked")




public void testHQLCastWhere() {






St
ringBuilder hqlQuery = new StringBuilder("select c.libelle from Courriel c where c.adresse =


cast('test@test.com' as varchar(100))");






Session session = hibernateTemplate.getSessionFactory().getCurrentSession();






Query query = session.createQuer
y(hqlQuery.toString());






List<String> list = query.list();











assertFalse(list.isEmpty());









}




These tests are OK!

Configuration 2

with where clause
:


Dialect
: org.hibernate.dialect.DB2Dialect

QueryTranslatorFactory
: org.hibernate.
hql.ast.ASTQueryTranslatorFactory (Default Factory in hibernate
3.3.2.GA)


Code:




@SuppressWarnings("unchecked")




public void testHQLCastWhere() {






StringBuilder hqlQuery = new StringBuilder("select c.libelle from Courriel c where c.adresse =


cas
t('test@test.com' as varchar(100))");






Session session = hibernateTemplate.getSessionFactory().getCurrentSession();






Query query = session.createQuery(hqlQuery.toString());






List<String> list = query.list();











assertFalse(list.isEm
pty());









}






or


Code:




@SuppressWarnings("unchecked")




public void testHQLCastWhere() {






StringBuilder hqlQuery = new StringBuilder("select c.libelle from Courriel c where c.adresse =


cast(?
as varchar(100))");






Session session =

hibernateTemplate.getSessionFactory().getCurrentSession();






Query query = session.createQuery(hqlQuery.toString());






query.setString(0, "test@test.com");






List<String> list = query.list();











assertFalse(list.isEmpty());









}



These tests are in error
:



Code:

org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 111 [select
c.libelle from gouv.education.ramsese.uai.Courriel c where c.adresse =


cast('test@test.com' as
varchar(100))]




at

org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)




at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)




at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)




a
t org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)




at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)




at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)




at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)




at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)




at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)




at org.hibernate
.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)




at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)




at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)




at gouv.education.rams
ese.test.HibernateTest.testHQLCastWhere(HibernateTest.java:114)


What's wrong ?