-->
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.  [ 6 posts ] 
Author Message
 Post subject: using createQuery(), HQL where clause dumped straight to SQL
PostPosted: Wed Sep 22, 2010 4:54 pm 
Beginner
Beginner

Joined: Tue Oct 04, 2005 8:09 am
Posts: 22
Location: Atlanta, USA
Hi everyone, I'm having trouble getting a simple HQL query to work.
The query's where clause is getting dumped straight into the generated SQL without translating field names into column names. For example, I've got a field called codeName mapped to the column CODE_NAME.

Original HQL:
from States where codeName = 'GA'

Generated SQL:
select states0_.STATE_ID as STATE1_65_, states0_.CODE_NAME as CODE2_65_, states0_.DESCRIPTION as DESCRIPT3_65_, states0_.DESCRIPTION_ES as DESCRIPT4_65_, states0_.LOS_CODE_NAME as LOS5_65_, states0_.SORT_ORDER as SORT6_65_ from STATES states0_ where (codeName='GA' )

This results in a db error since there's no column called CODENAME. I've confirmed by testing (output below) that the column really is mapped and that mapping makes the data in the CODE_NAME column available in the codeName field of persistent objects fetched from the db.

I'm using Hibernate 3.4.0.GA, mapping with annotations, and configuration via Spring 3.0.3.
Here are the mapped POJO, config, testing code, and testing output...

CONFIG
Code:
<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="testDataSource"></property>
      <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
      <property name="packagesToScan">
         <list>
            <value>com.gmacm.leads.model</value>
         </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.query.substitutions">true=1 false=0 yes='Y' no='N'</prop>
            <prop key="hibernate.show_sql">true</prop>
            <!-- Cache -->
            <prop key="hibernate.cache.region_prefix">hibernate.lm</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory
            </prop>

         </props>
      </property>
   </bean>
   <bean id="testDataSource"
      ------DB CONNECTION INFO REMOVED-----
   </bean>
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
   <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
   
   <context:component-scan base-package="com.gmacm.leads" />


ANNOTATED POJO
Code:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;




@Entity
@Table(name = "STATES")
public class States
implements java.io.Serializable {

   private String codeName;
   private String description;
   private String descriptionEs;
   private String losCodeName;
   private Long sortOrder;
   private long stateId;

   public States() {
   }

   public States(long stateId) {
      this.stateId = stateId;
   }

   public States(long stateId, Long sortOrder, String codeName,
         String description, String losCodeName, String descriptionEs) {
      this.stateId = stateId;
      this.sortOrder = sortOrder;
      this.codeName = codeName;
      this.description = description;
      this.losCodeName = losCodeName;
      this.descriptionEs = descriptionEs;
   }

   @Column(name = "CODE_NAME" )
   public String getCodeName() {
      return this.codeName;
   }

   @Column(name = "DESCRIPTION")
   public String getDescription() {
      return this.description;
   }

   @Column(name = "DESCRIPTION_ES")
   public String getDescriptionEs() {
      return this.descriptionEs;
   }

   @Column(name = "LOS_CODE_NAME")
   public String getLosCodeName() {
      return this.losCodeName;
   }

   @Column(name = "SORT_ORDER", precision = 10, scale = 0)
   public Long getSortOrder() {
      return this.sortOrder;
   }

   @Id
   @Column(name = "STATE_ID", unique = true, nullable = false, precision = 10, scale = 0)
   public long getStateId() {
      return this.stateId;
   }

   public void setCodeName(String codeName) {
      this.codeName = codeName;
   }

   public void setDescription(String description) {
      this.description = description;
   }

   public void setDescriptionEs(String descriptionEs) {
      this.descriptionEs = descriptionEs;
   }

   public void setLosCodeName(String losCodeName) {
      this.losCodeName = losCodeName;
   }

   public void setSortOrder(Long sortOrder) {
      this.sortOrder = sortOrder;
   }

   public void setStateId(long stateId) {
      this.stateId = stateId;
   }

}



TEST CODE
Code:


import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
//specifies the Spring configuration to load for this test fixture
@ContextConfiguration(locations={"classpath:TEST-leads-hibernate.xml"})
public class StatesTest {
   private static Logger log = Logger.getLogger(StatesTest.class);
   @Autowired SessionFactory sessionFactory;
   @Test public void sanity() throws Exception {}

   @Transactional   @Test public void allStates() {
      List<States> allStates = (List<States> )sessionFactory.getCurrentSession().createCriteria(States.class).list();
      for (States state: allStates) {
         log.info(state.getCodeName());
      }
      assert(null != allStates);
      assert(0 != allStates.size());
   }
   
        //Why do I fail?!?
   @Transactional @Test public void ga() {
      List gaList = sessionFactory.getCurrentSession().createQuery("from States where codeName = 'GA'").list();
      assert (null != gaList);
      assert gaList.size() == 1;
   }
}

TEST OUTPUT
Code:
Running com.gmacm.leads.model.StatesTest
Hibernate: select this_.STATE_ID as STATE1_65_0_, this_.CODE_NAME as CODE2_65_0_, this_.DESCRIPTION as DESCRIPT3_65_0_, this_.DESCRIPTION_ES as DESCRIPT4_65_
0_, this_.LOS_CODE_NAME as LOS5_65_0_, this_.SORT_ORDER as SORT6_65_0_ from STATES this_
INFO |09/22/10 16:42:43|701|AL
INFO |09/22/10 16:42:43|701|AK
INFO |09/22/10 16:42:43|701|AZ
INFO |09/22/10 16:42:43|701|AR
INFO |09/22/10 16:42:43|701|CA
INFO |09/22/10 16:42:43|701|CO
INFO |09/22/10 16:42:43|701|CT
INFO |09/22/10 16:42:43|701|DE
INFO |09/22/10 16:42:43|701|DC
INFO |09/22/10 16:42:43|701|FL
INFO |09/22/10 16:42:43|701|GA
INFO |09/22/10 16:42:43|701|HI
INFO |09/22/10 16:42:43|701|ID
INFO |09/22/10 16:42:43|701|IL
INFO |09/22/10 16:42:43|701|IN
INFO |09/22/10 16:42:43|701|IA
INFO |09/22/10 16:42:43|701|KS
INFO |09/22/10 16:42:43|701|KY
INFO |09/22/10 16:42:43|717|LA
INFO |09/22/10 16:42:43|717|ME
INFO |09/22/10 16:42:43|717|MD
INFO |09/22/10 16:42:43|717|MA
INFO |09/22/10 16:42:43|717|MI
INFO |09/22/10 16:42:43|717|MN
INFO |09/22/10 16:42:43|717|MS
INFO |09/22/10 16:42:43|717|MO
INFO |09/22/10 16:42:43|717|MT
INFO |09/22/10 16:42:43|717|NE
INFO |09/22/10 16:42:43|717|NV
INFO |09/22/10 16:42:43|717|NH
INFO |09/22/10 16:42:43|717|NJ
INFO |09/22/10 16:42:43|717|NM
INFO |09/22/10 16:42:43|717|NY
INFO |09/22/10 16:42:43|717|NC
INFO |09/22/10 16:42:43|717|ND
INFO |09/22/10 16:42:43|717|OH
INFO |09/22/10 16:42:43|717|OK
INFO |09/22/10 16:42:43|717|OR
INFO |09/22/10 16:42:43|717|PA
INFO |09/22/10 16:42:43|717|RI
INFO |09/22/10 16:42:43|717|SC
INFO |09/22/10 16:42:43|717|SD
INFO |09/22/10 16:42:43|717|TN
INFO |09/22/10 16:42:43|717|TX
INFO |09/22/10 16:42:43|717|UT
INFO |09/22/10 16:42:43|717|VT
INFO |09/22/10 16:42:43|717|VA
INFO |09/22/10 16:42:43|717|WA
INFO |09/22/10 16:42:43|717|WV
INFO |09/22/10 16:42:43|717|WI
INFO |09/22/10 16:42:43|717|WY
Hibernate: select states0_.STATE_ID as STATE1_65_, states0_.CODE_NAME as CODE2_65_, states0_.DESCRIPTION as DESCRIPT3_65_, states0_.DESCRIPTION_ES as DESCRIP
T4_65_, states0_.LOS_CODE_NAME as LOS5_65_, states0_.SORT_ORDER as SORT6_65_ from STATES states0_ where (codeName='GA' )
ERROR|09/22/10 16:42:44|311|ORA-00904: "CODENAME": invalid identifier



Top
 Profile  
 
 Post subject: Re: using createQuery(), HQL where clause dumped straight to SQL
PostPosted: Wed Sep 22, 2010 5:02 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
from States where codeName = 'GA'


I think you need to assign and use an alias for the entity (http://docs.jboss.org/hibernate/stable/ ... ryhql-from):

Code:
from States s where s.codeName = 'GA'


Top
 Profile  
 
 Post subject: Re: using createQuery(), HQL where clause dumped straight to SQL
PostPosted: Wed Sep 22, 2010 5:10 pm 
Beginner
Beginner

Joined: Tue Oct 04, 2005 8:09 am
Posts: 22
Location: Atlanta, USA
nordborg wrote:
I think you need to assign and use an alias for the entity (http://docs.jboss.org/hibernate/stable/ ... ryhql-from):


But the very first where clause example in that document (http://docs.jboss.org/hibernate/stable/ ... yhql-where) doesn't use an alias. it's just
that document wrote:
from Cat where name='Fritz'


Top
 Profile  
 
 Post subject: Re: using createQuery(), HQL where clause dumped straight to SQL
PostPosted: Thu Sep 23, 2010 2:03 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hmmm.... that was strange. Seems like the documentation is inconsistent. And when I try a query without an alias it works just fine. Don't know why it doesn't in your case.


Top
 Profile  
 
 Post subject: Re: using createQuery(), HQL where clause dumped straight to SQL
PostPosted: Thu Sep 23, 2010 11:08 am 
Beginner
Beginner

Joined: Tue Oct 04, 2005 8:09 am
Posts: 22
Location: Atlanta, USA
Thanks, nordborg.

So I can only assume that alias-less HQL queries like the one in the documentation are supported. I just have no clue how to tell why this one is failing.


Top
 Profile  
 
 Post subject: Re: using createQuery(), HQL where clause dumped straight to SQL
PostPosted: Thu Sep 23, 2010 1:49 pm 
Beginner
Beginner

Joined: Tue Oct 04, 2005 8:09 am
Posts: 22
Location: Atlanta, USA
*SOLVED*

The problem was this line in the config file above:
Code:
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>


That was a cut&paste error. Removing that property fixed the problem.


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