Is it possible to have optional property in ebean ORM

116 views Asked by At

When using Ebean, let's say, I have the ORM class as follows:

@Entity
public class Student {

    @id
    private UUID id;

    String age;
}

Assuming that the age is a nullable column in the database, we have to always check the null on this string. Is it possible to have age as an optional property to make this explicit?

@Entity
public class Student {

    @id
    private UUID id;

    Optional<String> age;
}
1

There are 1 answers

0
Rob Bygrave On

Is it possible to have age as an optional property to make this explicit?

No, it is not possible.

Note that we could modify Ebean to support this but we do not do that because we don't think this is the correct thing to do.

The reason is because it is generally expected that Optional isn't used for ANY FIELD in java. Optional is expected to be used as a return type or method parameter but NOT for a field. IntelliJ will show a warning on any field that uses an Optional type.

So Ebean does not support Optional fields because it would then be inconsistent with that general expectation that Optional should not be used this way.