1

我正在尝试在我的事务类(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>

所以,没有类型,但它不起作用。有没有人知道问题出在哪里?谢谢

4

2 回答 2

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();
于 2012-10-05T10:06:36.723 回答
1

将查询更改为 "SELECT DAT as \"dat\", DROP_RADIO as \"onRadio\" FROM My_TABLE"

于 2012-05-08T10:38:34.200 回答