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

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


    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 ===

    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 !?");
    nome = (Nome) nome.iterator().next();

    catch (HibernateException e)

    ======= It has a element in the DB ======= (Wished)

    .:. Console .:.
    Nomes: []
    E ae !?

    ======= 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:
    at java.util.AbstractList$ Source)
    at tlm.conexao.BancoDados.verificaNick(BancoDados.jav a:98)
    at tlm.ajax.VerificaNick.verificar( 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(
    at org.directwebremoting.impl.CreatorModule.executeMe thod(
    at org.directwebremoting.impl.DefaultRemoter.execute(
    at org.directwebremoting.impl.DefaultRemoter.execute(
    at org.directwebremoting.dwrp.BaseCallHandler.handle(
    at org.directwebremoting.servlet.UrlProcessor.handle(
    at org.directwebremoting.servlet.DwrServlet.doPost(Dw
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
    at ternalDoFilter(
    at Filter(
    at tlm.conexao.Filtro.doFilter(
    at ternalDoFilter(
    at Filter(
    at org.apache.catalina.core.StandardWrapperValve.invo ke(
    at org.apache.catalina.core.StandardContextValve.invo ke(
    at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.valves.ErrorReportValve.invoke (
    at org.apache.catalina.valves.AccessLogValve.invoke(A
    at org.apache.catalina.core.StandardEngineValve.invok e(
    at org.apache.catalina.connector.CoyoteAdapter.servic e(
    at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(
    at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(
    at java.util.concurrent.ThreadPoolExecutor$ Task(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$ (Unknown Source)
    at Source)

    Does Somebody know how to correct this error? Does Somebody have a alternative soluction ?


  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    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.
    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
    Rep Power

    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