aaberg / sql2o

sql2o is a small library, which makes it easy to convert the result of your sql-statements into objects. No resultset hacking required. Kind of like an orm, but without the sql-generation capabilities. Supports named parameters.
http://sql2o.org
MIT License
1.15k stars 229 forks source link

nullpointer exception fech complex object #272

Open devMls opened 7 years ago

devMls commented 7 years ago

Hi, I have this classes:

public class Pais implements Serializable {

   public void setIdpais(Integer idpais) {
        this.idpais = idpais;
    }
    public Integer getIdpais() {
        return idpais;
    }
}
public class Usuario implements Serializable {

    private Integer idusuario;
    private Pais pais;
    public Integer getIdusuario() {
        return idusuario;
    }

   public void setIdusuario(Integer idusuario) {
        this.idusuario = idusuario;
    }

    public Pais getPais() {
        return pais;
    }

    public void setPais(Pais pais) {
        this.pais = pais;
    }
}

running this query:

con.createPreparedQuery(" SELECT *    FROM usuario      where idusuario = :id")
                .addParameter("id", 1)
                .addColumnMapping("idpais", "pais.idpais")
                .executeAndFetch(Usuario.class);

i get this error:


ERROR [http-nio-8084-exec-223] (HttpSupport.java:69) - 
org.sql2o.Sql2oException: Could not map idpais to any property.
    at org.sql2o.DefaultResultSetHandlerFactory.newResultSetHandler0(DefaultResultSetHandlerFactory.java:199)
    at org.sql2o.DefaultResultSetHandlerFactory.access$200(DefaultResultSetHandlerFactory.java:17)
    at org.sql2o.DefaultResultSetHandlerFactory$5.evaluate(DefaultResultSetHandlerFactory.java:160)
    at org.sql2o.DefaultResultSetHandlerFactory$5.evaluate(DefaultResultSetHandlerFactory.java:156)
    at org.sql2o.tools.AbstractCache.get(AbstractCache.java:49)
    at org.sql2o.DefaultResultSetHandlerFactory.newResultSetHandler(DefaultResultSetHandlerFactory.java:173)
    at org.sql2o.PojoResultSetIterator.<init>(PojoResultSetIterator.java:20)
    at org.sql2o.Query$14.iterator(Query.java:547)
    at org.sql2o.Query.executeAndFetch(Query.java:588)
    at org.sql2o.Query.executeAndFetch(Query.java:574)

what is the correct way to do this?

devMls commented 7 years ago

Hi.

After read this:

https://groups.google.com/forum/#!topic/sql2o/eucKt1vVh9o

i achieve this changing the query:

SELECT nombre, idpais as "pais.idpais"
FROM usuario     
where idusuario = ?

and deleting the addcolumnMapping. in the post speak about the need of the public atributes, but that is not necessary in 1.6.rc3. Maybe is a problem with my concept of the columnMapping