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...
CONFIGCode:
<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 POJOCode:
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 CODECode:
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 OUTPUTCode:
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