-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Help printing the result of a query
PostPosted: Thu Dec 02, 2004 1:24 pm 
Newbie

Joined: Tue Nov 23, 2004 12:25 pm
Posts: 9
Hi for all...

I'm trying to print (System.out) the result of a single query through the code that follows:
-------------------------------------------------------------------------------
String sqlQuery = null;
sqlQuery ="FROM AMIGO in class HIBERNA.Amigo where Amigo.nome = 'lara'";
List lista = dao.getList(sqlQuery);
for(int indice = 0; indice < lista.size(); indice++)
System.out.println(lista.get(indice));
------------------------------------------------------------------------------
The DAO method is:

public java.util.List getList(String sqlQuery) throws Exception
{
Session session = factory.openSession();
List amigos = session.find(sqlQuery);
session.flush();
session.close();
return amigos;
}
-----------------------------------------------------------------------------
The sql-show returns...

Hibernate: select AMIGO.NOME as NOME, AMIGO.ENDERECO as ENDERECO, AMIGO.TELEFONE as TELEFONE, AMIGO.CELULAR as CELULAR, AMIGO.EMAIL as EMAIL from SCOTT.AMIGOS AMIGO where (Amigo.nome='lara' )

there is just one record on ORACLE and the command
if(dao.getList(sqlQuery).isEmpty() || dao.getList(sqlQuery) == null) show me 'FALSE'.
----------------------------------------------------------------------------------
However, i dont know how to print the entire result (even it be just one).
The line System.out.println(lista.get(indice)); dont sound like correct and the system prints 'HIBERNA.Amigo@ae533a'.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 2:20 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:57 am
Posts: 33
Location: Alpharetta, Georgia
Taken a look at
http://jakarta.apache.org/commons/lang/api/index.html

particularly the toStringBuilder stuff.

I use something like this in some of my unit tests to have a look at results

...
import org.apache.commons.lang.builder.*;
...
public void printObj(String msg, Object o) throws Exception {
System.out.println(objToString(msg,o));
}

public String objToString(String msg, Object o) throws Exception {
return msg + "\n" + objToString(o);
}

public String objToString(Object o) throws Exception {
return ReflectionToStringBuilder.toString(o,StandardToStringStyle.MULTI_LINE_STYLE,true);
}
....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 3:27 pm 
Newbie

Joined: Tue Nov 23, 2004 12:25 pm
Posts: 9
Thanks, Khote. I had try the follow (I'm not using webserver for now), and it works but i dont know if its the best way:

String sqlQuery = null;
sqlQuery ="FROM AMIGO in class HIBERNA.Amigo";
List lista = dao.getList(sqlQuery);

Amigo amigo1 = null;
for(int i = 0; i < lista.size(); i++)
{
amigo1 = (Amigo)lista.get(i);
System.out.println(amigo1.getCelular());
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 7:12 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:57 am
Posts: 33
Location: Alpharetta, Georgia
Inside your Amigo class you can override the toString() method as:

import org.apache.commons.lang.builder.*;
public class Amigo {
....
protected int id;
protected String name;
protected String code;
protected String celular;
....
public String toString() {
return new ToStringBuilder this,StandardToStringStyle.MULTI_LINE_STYLE)
.append("-- id", id)
.append("-- name", name)
.append("-- celular", celular)
.toString();
}
....
}

use the real properties in your Amigo class of course.
I know the "import org.apache.commons.lang.builder.*;" is Bad Form, but generally we're just using this kind of thing during development and debugging.
This commons-lang package is FULL of good stuff.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.