-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to display data from database
PostPosted: Fri Feb 23, 2007 3:25 am 
Newbie

Joined: Mon Feb 19, 2007 6:31 am
Posts: 12
Hi

Can someone help please?
I have a connection to my Posrgresql database.
How do i display the data that is contained in my list?
the part in bold does not work and give the error:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select name from NAMES
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
at hello.HelloWorld.main(HelloWorld.java:25)
5 message(s) found:

//this is part of my code.
Session newSession =HibernateUtil.currentSession ().getSessionFactory().openSession();
Transaction newTransaction = newSession.beginTransaction();
List messages = newSession.createSQLQuery("select name from NAMES").list();

//this tells me that my query found n items in the table
System.out.println( messages.size() + " message(s) found:" );

for ( Iterator iter = messages.listIterator();iter.hasNext(); )
{
NAMES loadedMsg = (NAMES) iter.next();
System.out.println( loadedMsg.getText() );
}
newTransaction.commit();
newSession.close();


//this is my NAMES class

package hello;

public class NAMES {
private long id;
private String text;
private NAMES nextMessage;

NAMES()
{

}

public NAMES(String text)
{
this.text = text;
}

public Long getId()
{
return id;
}

private void setId(Long id)
{
this.id = id;
}

public String getText()
{
return text;
}

public void setText(String text)
{
this.text = text;
}

public NAMES getNextMessage()
{
return nextMessage;
}

public void setNextMessage(NAMES nextMessage)
{
this.nextMessage = nextMessage;
}


}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 3:46 am 
Beginner
Beginner

Joined: Mon Jan 22, 2007 1:16 pm
Posts: 21
Which line in your code does the exception point to? The exception says
(HelloWorld.java:25), but which line is that?


I don't know if this will help you, but in my code i use an iterator :

for (Iterator iter = messages.iterator(); iter.hasNext();)


In your code, you have a listIterator:

for (Iterator iter = messages.listIterator(); iter.hasNext();)


regards,
Mark


Top
 Profile  
 
 Post subject: How to display data from database
PostPosted: Fri Feb 23, 2007 3:57 am 
Newbie

Joined: Mon Feb 19, 2007 6:31 am
Posts: 12
Hi Mark

this is the main class. i tried the changing ListIterator to just iterator but the problem still persists.

the problem:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select name from names
2 message(s) found:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
at hello.HelloWorld.main(HelloWorld.java:28)


package hello;

import java.util.*;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.util.*;
import org.hibernate.Session;
import java.util.logging.*;
import org.hibernate.dialect.PostgreSQLDialect;
import java.sql.*;



//import persistence.*;
public class HelloWorld {
public static void main(String[] args) {
// First unit of work

Session newSession =HibernateUtil.currentSession().getSessionFactory().openSession();
Transaction newTransaction = newSession.beginTransaction();
List messages = newSession.createSQLQuery("select name from names").list();

System.out.println( messages.size() + " message(s) found:" );

for ( Iterator iter = messages.iterator();iter.hasNext(); )
{
NAMES loadedMsg = (NAMES) iter.next();
System.out.println( loadedMsg.getText() );
}
newTransaction.commit();
newSession.close();

// Shutting down the application
HibernateUtil.closeSession();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 4:31 am 
Beginner
Beginner

Joined: Mon Jan 22, 2007 1:16 pm
Posts: 21
Ok. I see the problem now.

The problem is in your query.

List messages = newSession.createSQLQuery("select name from NAMES").list();

First, is there a reason you a creating an SQLQuery and not HQL?
I don't think your query is returning NAMES entities.


You should try this code which uses HQL:

List messages = newSession.createQuery(from NAMES").list();

This will return a List of NAMES entities



If you really need to use SQLQuery, then you need to do something like this:
List messages = newSession.createSQLQuery("select * from NAMES").addEntity("NAMES", NAMES.class ).list();


Top
 Profile  
 
 Post subject: How to display data from database
PostPosted: Fri Feb 23, 2007 4:54 am 
Newbie

Joined: Mon Feb 19, 2007 6:31 am
Posts: 12
Hi

thanks firstly. i tried the HQL and it return my data.
there is no reason for using the SQL opposed to HQL. i just it is better.
is there an advantage of using hql

if i use the SQL and i changed mine to use :
List messages = newSession.createSQLQuery("select * from NAMES").addEntity("NAMES", NAMES.class ).list();

i get the error:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select * from NAMES
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:1502)
at org.hibernate.loader.Loader.list(Loader.java:1482)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:103)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1333)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:146)
at hello.HelloWorld.main(HelloWorld.java:23)
Caused by: org.postgresql.util.PSQLException: The column name nameid0_ was not found in this ResultSet.
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.findColumn(AbstractJdbc2ResultSet.java:2450)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2317)
at org.hibernate.type.LongType.get(LongType.java:26)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:77)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:68)
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:650)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:277)
at org.hibernate.loader.Loader.doQuery(Loader.java:384)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:203)
at org.hibernate.loader.Loader.doList(Loader.java:1499)
... 5 more


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 5:13 am 
Beginner
Beginner

Joined: Mon Jan 22, 2007 1:16 pm
Posts: 21
This from the Hibernate documentation on HQL:
http://www.hibernate.org/hib_docs/v3/re ... ryhql.html

Hibernate is equipped with an extremely powerful query language that (quite intentionally) looks very much like SQL. But don't be fooled by the syntax; HQL is fully object-oriented, understanding notions like inheritence, polymorphism and association.

for the hibernate documentation on using SQL instead of HQL:
http://www.hibernate.org/hib_docs/v3/re ... rysql.html


I would only use SQL if there is something you can't do with HQL.

regards,
Mark


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 5:20 am 
Newbie

Joined: Mon Feb 19, 2007 6:31 am
Posts: 12
ok thanks for the resource links.
i really appreciate your help.

Cheers Avesh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.