Results 1 to 3 of 3
  1. #1
    LeoKringer is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default [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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    ** This space for rent **

  3. #3
    LeoKringer is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: [Hibernate] - Error using "session.createQuery().list()"

    Thank you soo much!!!

    I'm using "hasCode()" to get the element and I've resolved my problem!

    Somebody can close this top... Problem is resolved!

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 04:51 PM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 08:50 AM
  3. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  4. Replies: 0
    Last Post: 03-16-2009, 12:53 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •