null pointer exception in complex object

84 views
Skip to first unread message

lar...@gmail.com

unread,
Mar 13, 2017, 6:50:03 PM3/13/17
to sql2o

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?

Николай Митропольский

unread,
Mar 14, 2017, 4:37:37 AM3/14/17
to sql2o, lar...@gmail.com
Hello!

I think it is not possible to do this directly. Because sql2o is not an ORM and could not handle such complex things. But you cold try to add a converter class 
org.sql2o.converters.Converter

for Pais
to make it possible to create Pais instance from integers. 

вторник, 14 марта 2017 г., 1:50:03 UTC+3 пользователь lar...@gmail.com написал:

lar...@gmail.com

unread,
Mar 14, 2017, 12:02:51 PM3/14/17
to sql2o, lar...@gmail.com
Hi.

After read this:


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

Николай Митропольский

unread,
Mar 15, 2017, 1:12:57 AM3/15/17
to sql2o, lar...@gmail.com
hm, thats funny. I didn't know about it. About a column mapping it seems that splitting paths by dots is done before column mapping: DefaultResultSetHandlerFactory.java:29

вторник, 14 марта 2017 г., 19:02:51 UTC+3 пользователь lar...@gmail.com написал:
Reply all
Reply to author
Forward
0 new messages