aspose file tools*
The moose likes Object Relational Mapping and the fly likes getting List values out of hibernate query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "getting List values out of hibernate query" Watch "getting List values out of hibernate query" New topic
Author

getting List values out of hibernate query

jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
hi-

my latest project is to add expanded category views in a tree
structure for my checkbook program found at www.3rdshiftcoder.com

it seems that part of this query is mapped to a class but
the data from the rest of it is in java.util.List lst = query.list()

i cant figure how to get at the data in the lst so i can
build my tree structure!

can someone please help? code below:
tx = session.beginTransaction();
Query query = session.createSQLQuery("select {c.*} "+
",down1.description as down1 "+
",down2.description as down2 "+
",down3.description as down3 "+
"from CATEGORY as c "+
" left outer join CATEGORY as down1 on down1.parentid = c.categoryid" +
" left outer join CATEGORY as down2 on down2.parentid = down1.categoryid" +
" left outer join CATEGORY as down3 on down3.parentid = down2.categoryid " +
"where c.PARENTID is null "+
"order by c.description, down1, down2, down3")
.addEntity("c",Category.class);

System.out.println("before");
java.util.List lst = query.list();


for ( Iterator iter = lst.iterator();
iter.hasNext(); ) {
Category mycat = (Category) iter.next();

System.out.println(mycat.getCategoryid() + " mstr");
// i can get values out of mycat but need them out of the lst too!
//cant seem to go from a List value to a string
//i posted this in hibernate forum in case someone needed to understand the query and the issue.

thanks for any assistance,
jim
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
hi-

i am going to another group (java beginner) in the forum.
i want to see if i can narrow this down in its simplest test
and not involve any hibernate discussion yet.

thanks,
jim
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
hi-

i am going to another group (java beginner) in the forum.
i want to see if i can narrow this down in its simplest test
and not involve any hibernate discussion yet.

thanks,
jim
rahul khanna
Ranch Hand

Joined: Sep 14, 2005
Posts: 48
I did it some time back.. though I don't have the source code at the moment .. will have it tommorrow

try something on the lines of when you do a list() take the result into a list of objects.
Iterate the list( no of rows retrieved) and each object is the type casted into a object array (one row), the size of array being the number of parameters (columns in one row) retrieved in the sql query.

Iterate over the array and do obj[i].getString() to retrive each column with in the row.

Hope this helps.
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
hi Rahul-

if you run across a code sample, i'd really like to see it.
i have tried this 1,000 different ways.

i learned that when you perorm .toString() on an object it
doesnt behave like i thought it did. it puts the object
in a funny string code.
it seems the minute i do query.list().get(0) i am stuck.

i'm guessing i'd have to do some type of mapping with a
collection and use
the iterator to go through it. it doesnt look like there
is a quick fix.

i could be wrong....

thanks,
jim
jim mcnamara
Ranch Hand

Joined: Jun 13, 2007
Posts: 64
hi Rahul and everyone.
i can get it to work now.
i posted over in java beginners my proposed
solution. i had a mental block.
i cant believe how long i became stuck.
i know i havent coded java and hibernate
in over a 1/2 year but....

thanks for offering to help.
now a vision for a feature to my program
will get added!

jim
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting List values out of hibernate query