2

I have a database,i want to retrieve value from table whose id match with some id. My sample code is `

        public String getName() {SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
        Session session=sessionfactory.openSession();
        session.beginTransaction();
        Query qry=session.createQuery("select personaldetails.fname,personaldetails.lname from Personaldetails as personaldetails where refId=1001");
        List l=(List)qry.list();
        session.getTransaction().commit();
        session.close();
        for(int i=0;i<l.size();i++)
        {
            s=s+l.get(i).toString();
        }

        name=s;
        return name;
    }`

if i run this program it returns

[Ljava.lang.Object;@4b26fc[Ljava.lang.Object;@107ac1d[Ljava.lang.Object;@112d16

has output. But my database table contains 'David' 'billa'

2 Answers 2

1

IMHO, I define, in your case, an object formed by fname and lname. So your output isn't a List but a List, so you can manage better your information.

public class MyObject {
    public var lname;
    public var fname;

    public MyObject(String lName, String fName) {
        this.lname = lName;
        this.fname = fName;
    }
}


public String getName() {
    SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
    Session session=sessionfactory.openSession();
    session.beginTransaction();
    Query qry=session.createQuery("select new MyObject(personaldetails.fname,personaldetails.lname) from Personaldetails as personaldetails where refId=1001");
    List<MyObject> l=(List<MyObject>)qry.list();
    session.getTransaction().commit();
    session.close();
    for(MyObject curr : l)
    {
        // Here you can extract using property of MyObject
        s += curr.lname + " - " + curr.fname;
    }

    name=s;
    return name;
}
Sign up to request clarification or add additional context in comments.

2 Comments

i accept your point, but it shows ClassCastException at for(MyObject curr:l)
@PSK: I don't try in execution but the logic is ok. Try to show the result of query, peraphs don't convert correctly in MyObject.
1

By default toString() method of Object gets invoked.

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

That's why you get [Ljava.lang.Object;@4b26fc[Ljava.lang.Object;@107ac1d[Ljava.lang.Object;@112d16

You need to overwrite toString() method in Personaldetails entity in order to see David' & 'billa'. Should look like this:

@Override
public void String toString(){
    return this.firstName + " " + this.lastName;  
}

And you have to cast collection to appropriate list of entities

List<EntityType> list=(List<EntityType>)qry.list();

EntityType is a type of your entity.

Comments

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.