无法使用hibernate在正确的列表对象中填充数据库中的数据

时间:2014-01-17 13:12:30

标签: java hibernate hibernate-mapping

我有一个这样的数据库表:

gid     varchar     not null    primary key
cId     varchar     not null
guid    varchar 
d_flag  int         not null    
c_dt    datetime    
u_dt    datetime
d_dt    datetime

现在我想通过hibernate获取gid,cid guid和c_dt。

我已经配置了我的映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Julai 2010 11:40:18 AM by Hibernate Tools 3.2.5.Beta -->
<hibernate-mapping>
<class name="kmbt.csa.sboxm.model.SBoxInfo" 
    table="gwinfo" >
    <id name="id" type="org.hibernate.type.StringType" column="gid">                
    <generator class="assigned"/>
    </id>

     <property name="regTime" type="org.hibernate.type.TimestampType">
        <column name="c_dt" length="19" not-null="true" />
    </property>

    <property name="tenantId" type="org.hibernate.type.StringType">
        <column name="cId" length="30" not-null="true" />
    </property>

    <property name="gId" type="org.hibernate.type.StringType" insert="false" update="false">
        <column name="gid" length="100" not-null="true" />
    </property>

    <property name="gUserId" type="org.hibernate.type.StringType">
        <column name="guid" length="100" not-null="true" />
    </property>                      

</class>
</hibernate-mapping>

我的pojo课程:

public class SBoxInfo implements Serializable {

private static final long serialVersionUID = -4067221292770891832L;


    private int id; 
    private String regTime; 
    private String sLabelId; 
    private String tenantId;
    private String gId; 
    private String status; 
    private String stateChangedTime; 
    private String gUserId;





    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRegTime() {
        return regTime;
    }

    public void setRegTime(String regTime) {
        this.regTime = regTime;
    }

    public String getsLabelId() {
        return sLabelId;
    }

    public void setsLabelId(String sLabelId) {
        this.sLabelId = sLabelId;

    public String getTenantId() {
        return tenantId;
    }

    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }

    public String getGId() {
        return gId;
    }

    public void setGId(String gId) {
        this.gId = gId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStateChangedTime() {
        return stateChangedTime;
    }

    public void setStateChangedTime(String stateChangedTime) {
        this.stateChangedTime = stateChangedTime;
    }

    public String getGUserId() {
        return gUserId;
    }

    public void setGUserId(String gUserId) {
        this.gUserId = gUserId;
    }

}

现在我正在尝试获取这样的数据:

session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("select regTime,gwId,gwUserId,tenantId from SBoxInfo");
List<SBoxInfo> listOfSaaSGWs = (List<SBoxInfo>)query.list();
transaction.commit();
session.close();

但问题是我无法以List SBoxInfo格式获取数据,而是在简单对象中接收数据。

有人可以解释问题出在哪里吗?

1 个答案:

答案 0 :(得分:1)

只需将该查询更改为

即可
Query query = session.createQuery("from SBoxInfo");