-->
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.  [ 4 posts ] 
Author Message
 Post subject: Upgrade from 3.0.5 to 3.1 breaks queries
PostPosted: Fri Dec 16, 2005 12:12 pm 
Newbie

Joined: Fri Oct 14, 2005 2:35 pm
Posts: 14
I'm trying to upgrade from 3.0.5 to 3.1. I'm running into a problem where I've got a query like "from Contact where institution = ?" where institution is a property of Contact managed by Hibernate with a foreign key. It looks like Hibernate is just passing "institution" through straight thru to the sql query rather than doing a join with the institutions table.

Is this a bug in 3.1 or is it something that worked before and shouldn't have?

Hibernate version:
3.1

Mapping documents:
Contact.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="Contact" table="contacts">
...
        <many-to-one name="institution" column="institutionId" />
...
    </class>
</hibernate-mapping>


Institution.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="Institution" table="institutions">
...
    </class>
</hibernate-mapping>

Hibernate query:
Code:
from Contact where institution = ?


Full stack trace of any exception that occurs:
Code:
org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not execute query; bad SQL grammar [select contact0_.contactId as contactId6_, contact0_.firstName as firstName6_, contact0_.lastName as lastName6_, contact0_.primaryPhone as primaryP4_6_, contact0_.altPhone as altPhone6_, contact0_.street as street6_, contact0_.street2 as street7_6_, contact0_.city as city6_, contact0_.province as province6_, contact0_.postalCode as postalCode6_, contact0_.country as country6_, contact0_.salutation as salutation6_, contact0_.email as email6_, contact0_.gender as gender6_, contact0_.notes as notes6_, contact0_.institutionId as institu16_6_, contact0_.departmentId as departm17_6_, contact0_.contactTitleId as contact18_6_, CONCAT(contact0_.firstName, ' ', contact0_.lastName) as formula0_ from revman_contacts contact0_ where institution=? order by contact0_.lastName]; nested exception is java.sql.SQLException: Unknown column 'institution' in 'where clause'
java.sql.SQLException: Unknown column 'institution' in 'where clause'
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2926)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2978)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:933)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1027)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:91)
        at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
        at org.hibernate.loader.Loader.doQuery(Loader.java:662)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
        at org.hibernate.loader.Loader.doList(Loader.java:2150)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
        at org.hibernate.loader.Loader.list(Loader.java:2024)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:300)
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:146)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1093)
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
        at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:826)
        at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:365)
        at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:817)
        at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:813)
        at com.contentconnections.revman.spring.dao.ContactDaoHibernateSpringImpl.getByInstitution(ContactDaoHibernateSpringImpl.java:31)
        at com.contentconnections.revman.test.integration.spring.dao.ContactDaoHibernateSpringImplIntegrationTest.testGetByInstitution(ContactDaoHibernateSpringImplIntegrationTest.java:277)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)

Name and version of the database you are using:
MySQL 4.1.14

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 12:18 pm 
Beginner
Beginner

Joined: Mon Jul 05, 2004 9:29 am
Posts: 38
Could you try changing the query like this:

Code:
from Contact c where c.institution = ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 1:17 pm 
Newbie

Joined: Fri Oct 14, 2005 2:35 pm
Posts: 14
If I do that it works just fine. I'm just wondering why the change in 3.0.5 to 3.1. Is it a bug in Hibernate or a problem in my code?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 6:39 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
It seems to be a bug in the Hibernate 3 documentation, which indicates that "as" is an optional, but harmless, keyword in HQL. Actually, it causes an " unknown column in where clause " SQL Error.

(See the query section: http://www.hibernate.org/hib_docs/v3/re ... ryhql.html )

It would be a nice feature if, when "as" is used in HQL it reports this, since many of us probably have old 3.0 or 2.1 code that uses "as."


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