我正在尝试在我的事务类(Spring注释)中获取SQL查询的结果:
String query = "SELECT DAT, DROP_RADIO FROM My_TABLE";
SQLQuery q = getCurrentSession().createSQLQuery(query);
q.setResultTransformer(Transformers.aliasToBean(CallDrop.class));
List result = q.list();
和CallDrop:
public class CallDrop implements java.io.Serializable {
private int id;
private Date dat;
private BigDecimal onRadio;
public BigDecimal getOnRadio() {
return onRadio;
}
public void setOnRadio(BigDecimal onRadio) {
this.onRadio = onRadio;
}
public Date getDat() {
return dat;
}
public void setDat(Date dat) {
this.dat = dat;
}
}
.hbm.xml是:
<?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="com.company.beans.CallDrop" table="My_TABLE">
<id name="id" type="int" />
<property name="dat" type="date">
<column name="DAT" length="10" />
</property>
<property name="onRadio" type="big_decimal">
<column name="DROP_RADIO" precision="22" scale="0" />
</property>
</class>
</hibernate-mapping>
我确信我已在会话工厂中将hbm.xml添加到applicationContext文件中:
<bean id="sessionFactoryETL" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSourceETL"/></property>
<property name="mappingResources">
<list>
<value>com/company/beans/CallDrop.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
所以,没有类型,但它不起作用。有没有人知道这是什么问题? 感谢
答案 0 :(得分:3)
您可以使用addScalar(String columnAlias,Type type)显式声明本机SQL的列别名:
getCurrentSession().createSQLQuery("select e.id AS id, e.first_name AS firstName, e.password AS password FROM table")
.addScalar("id", StandardBasicTypes.INTEGER )
.addScalar("firstName", StandardBasicTypes.STRING )
.addScalar("password", StandardBasicTypes.STRING )
.setResultTransformer(Transformers.aliasToBean(Employee.class))
.list();
答案 1 :(得分:1)
将查询更改为"SELECT DAT as \"dat\", DROP_RADIO as \"onRadio\" FROM My_TABLE"