1
votes

I'm in the process of migrating from Play Framework 2.1.5 to 2.2.6. I was having tons of errors like this:

[error] C:\dev\CS\trunk\app\models\Asset.java:57: error: cannot find symbol
[error]         @NotNull
[error]          ^
[error]   symbol:   class NotNull
[error]   location: class Asset

and this:

play.PlayExceptions$CompilationException: Compilation error[error: package com.avaje.ebean.validation does not exist]
        at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14$$anonfun$apply$16.apply(PlayReloader.scala:304) ~[na:na]
        at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14$$anonfun$apply$16.apply(PlayReloader.scala:304) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
        at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14.apply(PlayReloader.scala:304) ~[na:na]
        at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14.apply(PlayReloader.scala:298) ~[na:na]

I have found online in this thread that I needed to add a line in my dependencies to make it work.

"org.avaje.ebeanorm" % "avaje-ebeanorm-api" % "3.1.1",

I have multiple question about this:

1. Why is another import needed for Ebean? I have imported the javaEbean, shouldn't it be enough to be up and running? It was ok 2.1.5 and nothing point to that in the migration documentation.

2. When I look at this package, it seems to be used by nobody. Should I be using something else? Is this some kind of deprecated package?

3. The main reason I ask all of these questions is because, even if it work to resolve these dependencies on my dev machine (windows), when deploying on the server (unix), it doesn't download the same "sub-dependencies" and it doesn't work at runtime. All of the log point to this library causing trouble...

Thanks!

1

1 Answers

1
votes

I have found my answer but it was a long journey, let's begin:

First, Ebean is a complete other project than Play Framework and between Play Framework 2.1 and Play Framework 2.2, the version from Ebean changed from 3.1.2 to 3.2.2. I could not found any release note or documentation but I have found a post from the author in a google group discussion that stated clearly that he deleted the validation from the Ebean library because people should use JSR 303 bean validation instead. Since Ebean is a one man show, the decision seems final.

The problem with that is that the library that provide this said bean validation (javax for example) are not called when doing an update() or a save() which is a regression in our code.

After numerous reading and testing, we have finally created a BaseModel based on Model that override the save() and update() method by calling a validator manully, like this:

@MappedSuperclass
public class BaseModel extends Model {

    @Override
    public void save() {
        Set<ConstraintViolation<BaseModel>> constraints = validate();

        if (constraints.size() > 0 ) {
            onFoundConstraints(constraints);
        } else {
            super.save();
        }
    }

    @Override
    public void update() {
        Set<ConstraintViolation<BaseModel>> constraints = validate();

        if (constraints.size() > 0 ) {
            onFoundConstraints(constraints);
        } else {
            super.update();
        }
    }

    private Set<ConstraintViolation<BaseModel>> validate() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        return validator.validate(this);
    }

    private void onFoundConstraints(Set<ConstraintViolation<BaseModel>> constraints) {
        String allErrors = "";
        for (ConstraintViolation<BaseModel> constraint : constraints) {
            allErrors += constraint.getRootBeanClass().getSimpleName()+
                    "." + constraint.getPropertyPath() + " " + constraint.getMessage();
        }

        throw new RuntimeException(allErrors);
    }
}

All our object now extends this class and it work well so far. I don't know if the previous validation was doing anything more than that but for our needs, it is ok.

I hope it help others since the Play Framework migration documentation doesn't even talk about this...