0

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?

1
  • List SBoxInfolist; SBoxInfolist= query .list(); for (int i = 0; i < SBoxInfolist.size(); i++) { SBoxInfo sBoxInfo= (SBoxInfo) SBoxInfolist.get(i); } Commented Jan 17, 2014 at 13:25

1 Answer 1

1

Just change that query to

Query query = session.createQuery("from SBoxInfo");
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried that too but when i change it like this, i get this error: IllegalArgumentException occurred while calling setter for property [kmbt.csa.sboxm.model.SBoxInfo.id (expected type = int)]; target = [kmbt.csa.sboxm.model.SBoxInfo@745f], property value = [0kyz4H6xS1NDmSvd9D0uAq26j49Dp5_6af548be-80b9-4c89-ba91-53f49e60ff83]
Isn't it that the from SBoxInfo will return all of the columns and as he wants only four of them.
i can't make any changes in database as database modifications are restricted.
I think there is no need of id in "SBoxInfo" and also remove id mapping to gId in mapping file , because you mapping gId in the class to tables gId .. then it will work
I've also tried that. When i remove the id mapping in mapping file i got this error: The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map| set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.