views:

27

answers:

1

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 = (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!

A: 

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

Saher
It looks overly complicated for a typical scenario met in a database application, don't you think? It looks like a quick fix.
Enzomatric