You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
OptionalRowMapperFactory doesn't change how underlying mappers interpret propagating nulls. So, a common use case will cause NullPointerException. If a SqlQuery returns an Optional single column row, the underlying mapper is called even if the column is null when a custom row mapper is registered for a type.
Example:
Test.java
packagetest;
importorg.jdbi.v3.core.Jdbi;
importorg.jdbi.v3.core.config.ConfigRegistry;
importorg.jdbi.v3.core.mapper.ColumnMapper;
importorg.jdbi.v3.core.mapper.RowMapper;
importorg.jdbi.v3.core.mapper.RowMapperFactory;
importorg.jdbi.v3.core.mapper.reflect.JdbiConstructor;
importorg.jdbi.v3.core.statement.StatementContext;
importorg.jdbi.v3.sqlobject.SqlObjectPlugin;
importorg.jdbi.v3.sqlobject.customizer.Bind;
importorg.jdbi.v3.sqlobject.statement.SqlQuery;
importorg.jdbi.v3.sqlobject.statement.SqlUpdate;
importorg.testcontainers.containers.PostgreSQLContainer;
importjava.lang.reflect.Type;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.Optional;
importstaticjava.util.Objects.requireNonNull;
publicclassTest
{
public record Value(Stringvalue)
{
@JdbiConstructorpublicValue
{
requireNonNull(value, "value is null");
}
}
publicinterfaceDao
{
@SqlQuery("SELECT value FROM foo where key = :key")
Optional<Value> get(@Bind("key") Stringkey);
@SqlUpdate("INSERT INTO foo(key, value) VALUES (:key, :value)")
voidinsert(@Bind("key") Stringkey, @Bind("value") Optional<Value> value);
}
publicstaticclassMapperimplementsRowMapperFactory, RowMapper<Value>
{
@OverridepublicOptional<RowMapper<?>> build(Typetype, ConfigRegistryconfig)
{
if (type.equals(Value.class)) {
returnOptional.of(this);
}
returnOptional.empty();
}
@OverridepublicValuemap(ResultSetrs, StatementContextctx)
throwsSQLException
{
Stringvalue = rs.getString("value");
returnnewValue(value);
}
}
publicstaticvoidmain(String[] args)
{
PostgreSQLContainercontainer = newPostgreSQLContainer();
container.start();
Jdbijdbi = Jdbi.create(container.getJdbcUrl(), container.getUsername(), container.getPassword())
.installPlugin(newSqlObjectPlugin())
.registerColumnMapper(Value.class, ColumnMapper.getDefaultColumnMapper())
.registerRowMapper((RowMapperFactory) newMapper());
jdbi.useHandle(handle -> handle.execute("CREATE TABLE foo (key VARCHAR NOT NULL PRIMARY KEY, value VARCHAR)"));
jdbi.useExtension(Dao.class, dao -> dao.insert("1", Optional.empty()));
// this will throw NPEOptional<Value> value = jdbi.withExtension(Dao.class, dao -> dao.get("1"));
container.stop();
}
}
OptionalRowMapperFactory
doesn't change how underlying mappers interpret propagating nulls. So, a common use case will causeNullPointerException
. If aSqlQuery
returns anOptional
single column row, the underlying mapper is called even if the column isnull
when a custom row mapper is registered for a type.Example:
Test.java
pom.xml
The text was updated successfully, but these errors were encountered: