Hi,
Enums should work, as long as the value in the database column is a string matching the name of the enum value.
If you wish to use other values to represent genders, such as numbers, there are at least two different approaches.
1.
Create a lookup table in your database, where your query can lookup the name of the enum, and join that table in your query. Your query will look something like this
from person p
inner join genders g on p.gender = g.gender_id
2.
Create a custom converter, so that sql2o knows how to translate numbers to gender. The converter will look something like this:
public class GenderConverter implements Converter<Gender> {
@Override
public Gender convert(Object val) throws ConverterException {
int genderId = (int)val;
switch (genderId) {
case 0:
return Gender.FEMALE;
case 1:
return Gender.MALE;
default:
throw new ConverterException("unknown gender");
}
}
@Override
public Object toDatabaseParam(Gender val) {
return val == Gender.FEMALE ? 0 : 1;
}
}
Then you can register the converter either by applying it to the quirks object when creating the Sql2o instance:
new Sql2o(ds, new NoQuirks(new HashMap<Class, Converter>(){{
put(Gender.class, new GenderConverter());
}}));
Or you can create a ConverterProvider class and register it with the
ServiceLoader in a file called:
resources/META-INF/services/org.sql2o.converters.ConvertersProvider
This will cause sql2o to automatically load the converter when it is initialized.
Hope this helps!
regards
Lars Aaberg