-->
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.  [ 3 posts ] 
Author Message
 Post subject: MySQLSyntaxErrorException: Can't use HQL to build queries?
PostPosted: Mon Mar 02, 2009 9:07 am 
Newbie

Joined: Mon Mar 02, 2009 8:26 am
Posts: 2
Location: Brasil
Hi,

I'm trying to use Hibernate to access a table I created into a local MySQL 5 instance. I've tried various diferent HQL statements, even a simple "from SakaiEvent sakaievent", but all of them end in the same error. I also tried all possible MySQL dialects listed here: http://www.hibernate.org/hib_docs/v3/re ... l-dialects plus org.hibernate.dialect.MySQL5Dialect, all with the same result. The only way I could access the base was using a SQL query.

Please note that for testing purposes I either comment the HQL query section or the SQL query one before compiling and running. I don't know if it is of any use, but I'm using Eclipse Ganymede and the mapping and cfg file were created via Hibernate Tools 3.2.4 Beta plugins.

Hibernate version: 3.3.1.GA
Name and version of the database you are using: MySQL 5.1.31 - community

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 01/03/2009 12:40:28 by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
<class name="databaseaccess.pojos.SakaiEvent" table="sakai_event" catalog="sakai_tidia">
<id name="eventId" type="long">
<column name="EVENT_ID" />
<generator class="identity" />
</id>
<property name="eventDate" type="timestamp">
<column name="EVENT_DATE" length="19" />
</property>
<property name="event" type="string">
<column name="EVENT" length="32" />
</property>
<property name="ref" type="string">
<column name="REF" />
</property>
<property name="sessionId" type="string">
<column name="SESSION_ID" length="163" />
</property>
<property name="eventCode" type="string">
<column name="EVENT_CODE" length="1" />
</property>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
// Query that results in error (idValue is determined a bit before this //code, it's a long.)
Query query = session.createQuery(
"select sakaiEvent " +
"from " +
"SakaiEvent sakaiEvent " +
"where " +
"sakaiEvent.eventId = :idVal");
query.setLong("idVal", idValue);

// Query that Works (EVENT_ID is just a ramdom number I came up with //for testing purposes)
// Query query = session.createSQLQuery(
// "SELECT sevent.EVENT_ID, " +
// "sevent.EVENT_DATE, " +
// "sevent.EVENT, " +
// "sevent.REF, " +
// "sevent.SESSION_ID, " +
// "sevent.EVENT_CODE " +
// "FROM Sakai_Event sevent " +
// "WHERE sevent.EVENT_ID = '200000'"
// ).addEntity(SakaiEvent.class);

List result = query.list();
session.getTransaction().commit();
session.close();

Full stack trace of any exception that occurs:

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:2231)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at control.StartHere.testOne(StartHere.java:45)
at control.StartHere.main(StartHere.java:17)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '.sakai_event sakaievent0_ where sakaievent0_.EVENT_ID=25' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1912)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
at org.hibernate.loader.Loader.doQuery(Loader.java:697)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
... 9 more


The generated SQL (show_sql=true):

Hibernate: select sakaievent0_.EVENT_ID as EVENT1_0_, sakaievent0_.EVENT_DATE as EVENT2_0_, sakaievent0_.EVENT as EVENT0_, sakaievent0_.REF as REF0_, sakaievent0_.SESSION_ID as SESSION5_0_, sakaievent0_.EVENT_CODE as EVENT6_0_ from sakai_tidia.sakai_tidia.sakai_event sakaievent0_ where sakaievent0_.EVENT_ID=?

NOTE:

When I use the SakaiEventHome generated by the Tools through the following code snippet:

SakaiEventHome home = new SakaiEventHome();
SakaiEvent evento = home.findById(25);

It ends in the same exeception as the code above.

So, am I missing something?

_________________
Irony Domine - Not Really...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 9:32 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
is it possible that you mixed up catalog and schema? Maybe in your hibernate.cfg.xml you have also mentioned a default schema?

Just to verify that: remove catalog="sakai_tidia" from your class mapping.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 9:39 am 
Newbie

Joined: Mon Mar 02, 2009 8:26 am
Posts: 2
Location: Brasil
It worked, mmerder!

Thank you very much.

I wouldn't think of that being the problem since the mapping was created via Hibernate tools (and I didn't specify any catalog at all).

The default schema is there in the cfg too.

_________________
Irony Domine - Not Really...


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