-->
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.  [ 12 posts ] 
Author Message
 Post subject: Query return null
PostPosted: Thu Jan 15, 2009 6:54 am 
Newbie

Joined: Thu Jan 15, 2009 6:41 am
Posts: 9
When i do this query Criteria put my pr object to null, "usuario" and "codigo" vars found correctly, I use manually the Criteria query in mysql command line with "usuario" and "codigo" values copy directly of debug and found without problems.

Hibernate version: 3

Mapping documents:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.destrozatumente.utils.objetos.PendienteRegistro" table="pendiente_registro">
        <id column="usuario" name="usuario" type="string"/>
        <property column="contrasena" name="contrasena" not-null="true" type="string"/>
        <property column="email" name="email" not-null="true" type="string"/>
        <property column="codigo" name="codigo" not-null="true" type="string"/>
    </class>
</hibernate-mapping>


Code:
package com.destrozatumente.utils.objetos;

public class PendienteRegistro {

    private String usuario;
    private String contrasena;
    private String email;
    private String codigo;

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getContrasena() {
        return contrasena;
    }

    public void setContrasena(String contrasena) {
        this.contrasena = contrasena;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }
}


Code between sessionFactory.openSession() and session.close():

Code:
    public PendienteRegistro penReg(String usuario, String codigo) {
        Persistencia ps = new Persistencia();
        Session ss = ps.getSession();
        ss.beginTransaction();

        PendienteRegistro pr = new PendienteRegistro();

        try {
            Criteria crit = ss.createCriteria(PendienteRegistro.class);
            pr = (PendienteRegistro) crit.add(Restrictions.like("usuario", usuario)).add(Restrictions.like("codigo", codigo)).uniqueResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ss.close();
       
        return pr;
    }


Full stack trace of any exception that occurs: Nothing

Name and version of the database you are using: Mysql v5.1.11

The generated SQL (show_sql=true):

Code:
Hibernate: select this_.usuario as usuario1_0_, this_.contrasena as contrasena1_0_, this_.email as email1_0_, this_.codigo as codigo1_0_ from pendiente_registro this_ where this_.usuario like ? and this_.codigo like ?


Debug level Hibernate log excerpt: I dont know


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 1:40 pm 
Newbie

Joined: Thu Jan 15, 2009 6:41 am
Posts: 9
Somebody can help me please? Is important.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 2:56 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Are you sure the variables are passed to the query correctly?

Try to remove the like-Restrictions and just do one with a known id... which just made me realize that your are using "usuario" as the id, usually not the best solution as it is a string

You can also try to replace the criteria query with HQL
Code:
ss.createQuery("from PendienteRegistro where usuario like :usuario and codigo like :codigo").addString("usuario",usuario).addString("codigo",codigo).uniqueResult();

and if you begin a Transaction you should commit at the end before closing the session.

hope that helps


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 5:19 pm 
Newbie

Joined: Thu Jan 15, 2009 6:41 am
Posts: 9
pkleindl wrote:
Are you sure the variables are passed to the query correctly?

Try to remove the like-Restrictions and just do one with a known id... which just made me realize that your are using "usuario" as the id, usually not the best solution as it is a string

You can also try to replace the criteria query with HQL
Code:
ss.createQuery("from PendienteRegistro where usuario like :usuario and codigo like :codigo").addString("usuario",usuario).addString("codigo",codigo).uniqueResult();

and if you begin a Transaction you should commit at the end before closing the session.

hope that helps


Hi pkleindl and thanks very much for your help!

First, yes, "usuario" and "codigo" values are correct, check in real time debug.

I tried other Hibernate method to check if only failed Criteria method or other too.

I tried this:

Code:
Query query = ss.createSQLQuery("select * from pendiente_registro where usuario like ? and codigo like ? ").addEntity(PendienteRegistro.class);
            query.setString(0, usuario);
            query.setString(1, codigo);
            pr = (PendienteRegistro) query.uniqueResult();


But query return "null" too, if I try with this:

Code:
Query query = ss.createSQLQuery("select * from pendiente_registro where usuario like 'value' and codigo like 'value' ").addEntity(PendienteRegistro.class);
pr = (PendienteRegistro) query.uniqueResult();


Result is correct, found correctly.

All interesting question is that if I copy and paste Hibernate sql_show print in java console, and replace '?' by correct values in mysql command line, query is correct too.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 6:14 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Did you try the HQL i suggested?

If the only difference of your SQL queries is the setting of params then i'm out of ideas, those should produce the same results.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 6:26 pm 
Newbie

Joined: Thu Jan 15, 2009 6:41 am
Posts: 9
pkleindl wrote:
Did you try the HQL i suggested?

If the only difference of your SQL queries is the setting of params then i'm out of ideas, those should produce the same results.


.addString not found... Want you tell me .setString?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 6:43 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
yes setString, remembered it wrong


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 8:03 pm 
Newbie

Joined: Thu Jan 15, 2009 6:41 am
Posts: 9
pkleindl wrote:
yes setString, remembered it wrong


This Query, with .addString or .setString give me a syntax error..., please, can you give me this HQL well?

Thank you again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 4:47 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Please post the error message, i did not try that statement, just wrote it from what i remembered.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 6:17 pm 
Newbie

Joined: Thu Jan 15, 2009 6:41 am
Posts: 9
pkleindl wrote:
Please post the error message, i did not try that statement, just wrote it from what i remembered.


Hi pkleindl, I solved my problem... I was put vars pass by GET url between '', and I not see this in debug mode... very stupid by true...

Thank you for your help and sorry by my nonsense, +1 credits.


Top
 Profile  
 
 Post subject: Brass air fittings
PostPosted: Fri Jan 16, 2009 9:24 pm 
Newbie

Joined: Fri Jan 16, 2009 3:11 am
Posts: 1
Now that is a lot of brass air fittings http://www.liangdianup.com/subpages/airfitting_1.htm there is just about every type
of air fitting that you could want. Wholesale prices too. I guess these could be used as small water pipe fitting also. I
used some of the parts to make my babington wvo burner.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 2:00 am 
Newbie

Joined: Tue Feb 10, 2009 10:50 pm
Posts: 1
Our team is also experiencing (almost?) the same problem right now but under different circumstances:

We are executing a (very long) native SQL statement via createSQLQuery and we found out that it returns null values for a specific field (bugTimeBeforeShipment) in the (ProjectReport) table.


Hibernate version: 3

Mapping documents:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.blahblah.entity">
   <class
      name="ProjectReportModel"
      table="project_report"
   >
      <meta attribute="sync-DAO">false</meta>
      <id
         name="reportId"
         type="string"
         column="report_id"
      >

      </id>

...

      <property
         name="bugTimeBeforeShipment"
         column="bug_time_before_shipment"
         type="big_decimal"
         not-null="false"
         length="4"
      />
      <property
         name="bugTimeAfterShipment"
         column="bug_time_after_shipment"
         type="big_decimal"
         not-null="false"
         length="4"
      />

...

   </class>   
</hibernate-mapping>


...

Code:
public abstract class BaseProjectReportModel  implements Serializable {

...

   // constructors
   public BaseProjectReportModel () {
      initialize();
   }

...

   /**
    * Return the value associated with the column: bug_time_before_shipment
    */
   public java.math.BigDecimal getBugTimeBeforeShipment () {
      return bugTimeBeforeShipment;
   }

   /**
    * Set the value related to the column: bug_time_before_shipment
    * @param bugTimeBeforeShipment the bug_time_before_shipment value
    */
   public void setBugTimeBeforeShipment (java.math.BigDecimal bugTimeBeforeShipment) {
      this.bugTimeBeforeShipment = bugTimeBeforeShipment;
   }

   /**
    * Return the value associated with the column: bug_time_after_shipment
    */
   public java.math.BigDecimal getBugTimeAfterShipment () {
      return bugTimeAfterShipment;
   }

   /**
    * Set the value related to the column: bug_time_after_shipment
    * @param bugTimeAfterShipment the bug_time_after_shipment value
    */
   public void setBugTimeAfterShipment (java.math.BigDecimal bugTimeAfterShipment) {
      this.bugTimeAfterShipment = bugTimeAfterShipment;
   }

...

}


Code between sessionFactory.openSession() and session.close():

...
Iterator<Object[]> result = getSession().createSQLQuery(sql.toString()).list().iterator();
...

Full stack trace of any exception that occurs: Nothing

Name and version of the database you are using: PostgreSQL 8.1.10


The generated SQL (show_sql=true):

it's too long to post here but the point is we copied the sql in the log file
to pgadmin (and even the pg console) and it shows all the values correctly

Debug level Hibernate log excerpt: Nothing

Any valuable help would be greatly appreciated.
Please tell us immediately if we are posting wrong/lacking information
Thanks.


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