Closed dicksonleong closed 5 years ago
sure will have a look later today.
so checked, there is the possibility to add an IndexedSetterProperty on a column
Crud<FooInet, Long> crud =
JdbcMapperFactory
.newInstance()
.addColumnProperty("b", IndexedSetterProperty.<InetAddress>of((ps, i, t) -> ps.setObject(i, t.getHostAddress(), Types.OTHER)))
.crud(FooInet.class, long.class)
.table(c, "issue_649");
I think that should solve your problem it's not as nice as the getter side, butI'll try to add a addSetter for type in the next release
I tried with SqlParameterSourceFactory but it doesn't seems like working, did I do something wrong?
private val sourceFactory = JdbcTemplateMapperFactory.newInstance()
.addColumnProperty("status", IndexedSetterProperty.of<Status> { ps, i, value ->
ps.setInt(i, value.persistValue) // this line doesn't trigger on breakpoint
})
.newSqlParameterSourceFactory(MyEntity::class.java)
sourceFactory.newSqlParameterSource(myEntity).getValue("status") // => Status
no I digged a bit deeper, and realized it was not going through what I thought it would. Right now I don't believe there is a way to do that, will make it possible.
so first try I was thinking of adding a ConverterProperty like
SqlParameterSourceFactory<Issue675> sourceFactory = JdbcTemplateMapperFactory.newInstance()
.addColumnProperty("myEnum", new ConverterProperty<MyEnum, Long>(new ContextualConverter<MyEnum, Long>() {
@Override
public Long convert(MyEnum in, Context context) throws Exception {
switch (in) {
case ONE: return 1l;
case TWO: return 2l;
default: throw new IllegalArgumentException();
}
}
}, MyEnum.class))
.newSqlParameterSourceFactory(Issue675.class, query);
it's a bit awkward because the parameter source is not directly link to the preparedstatement so it can't go through the setterproperty, and the getterproperty is about the result set. what do you think?
Looks okay.. but is it possible to specify a "global" converter like the one you suggested in here?
by global you mean link to the type instead of the column? yes so it should work as it check the convert type so if you apply ot all column
.addColumnProperty(k -> true, new ConverterProperty<MyEnum, Long>(new ContextualConverter<MyEnum, Long>() {
@Override
public Long convert(MyEnum in, Context context) throws Exception {
switch (in) {
case ONE: return 1l;
case TWO: return 2l;
default: throw new IllegalArgumentException();
}
}
}, MyEnum.class))
it should apply to all MyEnum.class
Hi, it's me again. Last time i tried to map Database column to Enum and it works, but now I want to map back Enum (implemented a common interface) back to database column (Int type) using
SqlParameterSourceFactory
.. But I can't find any documentation on this. Can you help?