Sometime last year, when I started using JPA with Hibernate (and MySQL), I received the following exception:
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 '2 textentity0_.TEXT_ENVELOPE_ID as
TEXT1_0_, textentity0_.PATH as PATH0_, tex' at line 1
The line that blew up is highlighted below:
-
StrBuilder hql = new StrBuilder(100);
- hql.append(" FROM TextEnvelope textEnvelope ");
- hql.append(" WHERE textEnvelope.entityPath.containerPath.path = :path ");
-
hql.append(" AND textEnvelope.entityPath.entityName = :entityName ");
-
- Query hqlQuery = entityManager.createQuery(hql.toString());
- hqlQuery.setParameter("path", entityPath.getContainerPath().getPath());
- hqlQuery.setParameter("entityName", entityPath.getEntityName());
-
- TextEnvelope result = hqlQuery.getSingleResult();
Odd that this worked for another query that didn't return a TextEnvelope.
On further inspection, I noticed the working query's mapping file didn't
use a Hibernate component, whereas the problem query did.
-
<hibernate-mapping package="com.thruzero.domain.model" default-access="field">
-
<class name="TextEnvelope" table="TEXT_ENVELOPE">
-
<id name="id" column="TEXT_ENVELOPE_ID" type="long">
-
<generator class="increment" />
- </id>
-
-
<component name="entityPath" class="com.thruzero.common.core.support.EntityPath">
-
<component name="containerPath" class="com.thruzero.common.core.support.ContainerPath">
-
<property name="path" column="PATH" />
- </component>
-
<property name="entityName" column="NAME" />
- </component>
-
-
<property name="data" type="string" column="DATA" />
- </class>
- </hibernate-mapping>
I also noticed that the component didn't cause a problem when getResultList() was used.
So here's the hack I ended up with:
- TextEnvelope result = JpaUtils.getSingleResultHack(hqlQuery);
Where JpaUtils.getSingleResultHack is implemented as follows:
- public static T getSingleResultHack(Query hqlQuery) {
- T result = null;
- List hack = hqlQuery.getResultList();
-
- if (!hack.isEmpty()) {
-
if (hack.size() > 1) {
-
throw new DAOException("ERROR: getSingleResultHack found more than one result.");
- }
- result = hack.get(0);
- }
-
- return result;
- }
I'm not sure if this is a bug or a feature, but until I find a better work-around...
-Geo.