Results 1 to 3 of 3
- 04-20-2012, 05:05 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
[Hibernate] - Error using "session.createQuery().list()"
Hi,
I'm trying to make a query using hibernate + mysql. Actually the problem is the return function of the DB...
When it has a element in the DB, the function returns this element and everything works correctly. But, when it doesn't have the element in the DB, the function returns "[]" and nothing works.
Follow the code and results...
=== Query ===
try
{
transaction = session.beginTransaction();
List nomes = session.createQuery("from Nome where nome = '" + nome + "'").list();
System.out.println("Nomes: " + nomes);
if (nomes != null)
{
System.out.println("E ae !?");
System.out.println(nomes.iterator().next());
nome = (Nome) nome.iterator().next();
System.out.println(nome);
}
transaction.commit();
}
catch (HibernateException e)
{
transaction.rollback();
e.printStackTrace();
}
======= It has a element in the DB ======= (Wished)
.:. Console .:.
Nomes: [tlm.bd.Nome@2470202e]
E ae !?
tlm.bd.Nome@2470202e
tlm.bd.Nome@2470202e
======= It doen't hava a element in the DB ======= (It's also wished)
.:. Console .:.
Nomes: []
E ae !?
19/04/2012 20:57:56 org.directwebremoting.impl.DefaultRemoter writeExceptionToAccessLog
INFO: Method execution failed:
java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(Unknown Source)
at tlm.conexao.BancoDados.verificaNick(BancoDados.jav a:98)
at tlm.ajax.VerificaNick.verificar(VerificaNick.java: 13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.directwebremoting.impl.CreatorModule$1.doFilte r(CreatorModule.java:229)
at org.directwebremoting.impl.CreatorModule.executeMe thod(CreatorModule.java:241)
at org.directwebremoting.impl.DefaultRemoter.execute( DefaultRemoter.java:379)
at org.directwebremoting.impl.DefaultRemoter.execute( DefaultRemoter.java:332)
at org.directwebremoting.dwrp.BaseCallHandler.handle( BaseCallHandler.java:104)
at org.directwebremoting.servlet.UrlProcessor.handle( UrlProcessor.java:120)
at org.directwebremoting.servlet.DwrServlet.doPost(Dw rServlet.java:141)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
at tlm.conexao.Filtro.doFilter(Filtro.java:48)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor$Worker.run Task(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
at java.lang.Thread.run(Unknown Source)
Does Somebody know how to correct this error? Does Somebody have a alternative soluction ?
Thanks,
Leo
- 04-23-2012, 11:22 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: [Hibernate] - Error using "session.createQuery().list()"
Please use [code] tags [/code] when posting code.
Iterator has a method that lets you check whether there is another element in the List.
hasNext().
The other option is to simply check the size of the List. There's a method on List for that.
Since you only want the one element then an Iterator seems a bit pointless.
Of course Query has a method that returns a unique instance or null, which seems to be what you're looking for.
In short, the lesson here is read the API.Please do not ask for code as refusal often offends.
- 04-23-2012, 03:08 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
problem with argument list and precedence "(" and ")"
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-24-2009, 07:50 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
"Cached Item Was Locked" causing Select Statement: Hibernate + EHCache
By cloutierm in forum Advanced JavaReplies: 0Last Post: 03-15-2009, 11:53 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks