Play Framework: add a custom Scalar Type in Ebean

2 min read
Unfortunately within the Ebean docs is not so clear how to redefine a scalar type. We made some experiments and it is quite simple though. In our example here we've created a new class to extend the classic java.util.Date.

You just have to create a new class that implements the conversion using the ScalarTypeConverter interface and put it into the models folder.

public class MyDate implements ScalarTypeConverter<MyDate, Date> {

@Override
public DataOra getNullValue() {
return null;
}

@Override
public MyDate wrapValue(Date scalarType) {
return new MyDate(scalarType);
}

@Override
public Date unwrapValue(MyDate beanType) {
return beanType;
}
}