Wednesday, May 7, 2014

Play2 PersistenceException

You might come across the error:
[PersistenceException: Error loading on models.Period.startYear]
in Play 2 Framework.
This is most likely caused by your database having a NULLable field which doesn't map to a nullable member in the model.
E.g.:

You database has the following fields:
ADD COLUMN `start_year` INT(11) NULL DEFAULT NULL;
ADD COLUMN `end_year` INT(11) NULL DEFAULT NULL;

 which means start_year may be (null) or an int.

Your java model has the following members:
@Entity
public class Period extends Model {
    @Id
    public Long periodId;

    @Required
    public int startYear;
   
    @Required
    public int endYear;
}

Which means that startYear may be an int, but not null.

To fix, make change the database fields to not be nullable.
Or the java model members nullable also, by using the Integer object wrapper instead of int:
@Entity
public class Period extends Model {
    @Id
    public Long periodId;

    @Required
    public Integer startYear;
   
    @Required
    public Integer endYear;
}


See: http://stackoverflow.com/questions/18405141/persistenceexception-when-i-was-using-ebean-in-play-framework

No comments:

Post a Comment