Prashant Sehta
Prashant Sehta

Reputation: 33

Unable to populate data from database in proper list object using hibernate

I have a database table like this:

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

now i want to fetch gid, cid guid and c_dt through hibernate.

I've configured my mapping file like this:

<?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>

My pojo class:

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;
    }

}

Now I'm trying to fetch the data like this:

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();

But the problem is I'm not able to fetch the data in List SBoxInfo format instead it is receiving data in the simple Object.

Can anybody explain where is the problem?

Upvotes: 0

Views: 718

Answers (1)

mahesh
mahesh

Reputation: 1331

Just change that query to

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

Upvotes: 1

Related Questions