aaberg/sql2o

Retrieving List<Long> from query

Closed this issue · 2 comments

We have a query that returns a List<Long>. Im in the process of upgrading to JDK21 along with sql1o v1.8 - was this constructor removed in a JDK after 8?

org.sql2o.Sql2oException: Could not find parameter-less constructor of class class java.lang.Long

at org.sql2o.reflection.ReflectionObjectConstructorFactory.newConstructor(ReflectionObjectConstructorFactory.java:24)
at org.sql2o.reflection.FactoryFacade.newConstructor(FactoryFacade.java:80)
at org.sql2o.reflection.PojoMetadata.initializePropertyInfo(PojoMetadata.java:105)
at org.sql2o.reflection.PojoMetadata$Cache.evaluate(PojoMetadata.java:250)
at org.sql2o.reflection.PojoMetadata$Cache.evaluate(PojoMetadata.java:247)
at org.sql2o.tools.AbstractCache.get(AbstractCache.java:49)
at org.sql2o.reflection.PojoMetadata.getPropertyInfoThroughCache(PojoMetadata.java:87)
at org.sql2o.reflection.PojoMetadata.<init>(PojoMetadata.java:70)
at org.sql2o.DefaultResultSetHandlerFactoryBuilder.newFactory(DefaultResultSetHandlerFactoryBuilder.java:68)
at org.sql2o.Query.newResultSetHandlerFactory(Query.java:547)
at org.sql2o.Query.executeAndFetch(Query.java:613)

The executeAndFetch method expects a model class as a parameter. It expects to use the built-in converter framework to convert the fetched database values to the properties in the model class. It doesn't expect to create a simple list of primitives, as in your example.

The method you are looking for is the executeScalarList method. If you use that, it should work as you expect in your example.

List<Long> longList = myQuery.executeScalarList(Long.class);

Thanks - this used to work just fine in our version - we must have patched this so that the API was simpler /more consistent. We know the return type - why have two methods to get at a List<> of something