开发者

Hibernate query result

开发者 https://www.devze.com 2023-01-10 13:35 出处:网络
I have the following query in my application:v public List getLocation(String id) { List object = null;

I have the following query in my application:v

public List getLocation(String id) {
    List object = null;
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        Query q = session.createQuery("from Entrancestolocations as EL left join EL.entrances as E left join EL.location as L where L = " + id);

        object = (开发者_JAVA技巧List) q.list();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return object;
}

The result of this query is a list . Entrancestolocations is a table with foreign key to location and entrances. I have no idea how to access the elements of this list without using the indexes as I don't know what is the type of objects that I deal with. The implementation is messed up - how to do it correctly? Which kind of object should I use to store the data from different tables from the database to be able to use them in an application? Thanks for help!


I worked for a month with Hibernate only but I think that you should maybe use a List of an array of objects:

    List<Object[]> objects = null;

add a Map: Map myMap = new HashMap();

For example, if you want to access the results by field x : simply add that to your query.

...CreateQuery("Select Table.x Table2.y from ..... where id = ");

try { org.hibernate.Transaction tx = session.beginTransaction(); Query q = session.createQuery("from Entrancestolocations as EL left join EL.entrances as E left join EL.location as L where L = " + id);

    objects = (List<Object[]>) q.list();

} catch (Exception e) {
    e.printStackTrace();
}

for(Object[] o : objects){// o[0] is each x of Table1 and o[1] is each y of Table2
 myMap[(String)o[0]] = (String)o[1];

}

return myMap;

you can now access your map by the x attribute for example..

0

精彩评论

暂无评论...
验证码 换一张
取 消