Query syntax error?

150 views
Skip to first unread message

tianhsky

unread,
Feb 21, 2012, 1:45:33 PM2/21/12
to Querydsl
I have successfully generated Data models using Hibernate reverse
engineering, and generated Qmodels using HibernateDomainExporter, so I
am assuming all my configurations are correct;
I am trying to test the query using the following code:

Session session = new
Configuration().configure().buildSessionFactory().openSession();
HibernateQuery query = new HibernateQuery(session);
QUser user = QUser.user;
User u =
query.from(user).where(user.userName.eq("test")).uniqueResult(user);

----------------------------------------
It gives me error:

Exception in thread "main"
org.hibernate.exception.SQLGrammarException: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near '.user user0_ where
user0_.userName='test'' at line 1
at
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:
122)
at
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:
47)
at
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:
125)
at
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:
110)
at
org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:
129)
at
org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:
81)
at $Proxy5.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1978)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
289)
at org.hibernate.loader.Loader.doList(Loader.java:2463)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
at org.hibernate.loader.Loader.list(Loader.java:2274)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at
org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:
355)
at
org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:
196)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1115)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at
org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:
890)
at
com.mysema.query.jpa.hibernate.AbstractHibernateQuery.uniqueResult(AbstractHibernateQuery.java:
406)
at
com.mysema.query.jpa.hibernate.AbstractHibernateQuery.uniqueResult(AbstractHibernateQuery.java:
391)
at Test.testQmodel(Test.java:49)
at Test.main(Test.java:58)


---------------------

Timo Westkämper

unread,
Feb 21, 2012, 4:17:41 PM2/21/12
to quer...@googlegroups.com
Hi.

Could you provide the full SQL query? Maybe also the full JPQL query.

This might be an issue with using a reserved keyword which unfortunately Hibernate and Querydsl don't protect you from.

Br,
Timo
--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com



tianhsky

unread,
Feb 22, 2012, 9:40:26 AM2/22/12
to Querydsl
Thanks, after I config hibernate.cfg.xml to show query using <property
name="hibernate.show_sql">true</property>,
I found the query is wrong:
select
user0_.id as id0_,
user0_.userName as userName0_,
user0_.password as password0_
from
test.test.user user0_
where
user0_.userName=?

it goes to test.test.user instead of test.user, it is because the
hibernate.cfg.xml is wrong:
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/
test</property>
<property name="hibernate.default_schema">test</property>
just remove the default_schema property would fix it.

I think the way querydsl finds the schema is: "connection.url" +
"default_schema".




On Feb 21, 4:17 pm, Timo Westkämper <timo.westkam...@mysema.com>
wrote:

Timo Westkämper

unread,
Feb 22, 2012, 9:46:02 AM2/22/12
to quer...@googlegroups.com
Querydsl actually uses a JPQL query to query through Hibernate.

Something like this

  select user from User user where user.username = ?

Hibernate translates this JPQL query into the SQL query you just posted.

Br,
Timo
Reply all
Reply to author
Forward
0 new messages