PersistenceException when I was using Ebean in Play framework
Asked Answered
H

1

5

I am writing a web application in Java by using Play Framework 2.1.0. And I am using Ebean to manipulate the database. But I got one problem now:

I have a model class called book with an int type column called page:

    public int page;

    @Column(name="page")
    public int getPage() {
        return page;
    }

   public void setPage(int page) {
        this.page = page;
   }

And the page column in my MySql database is also int type. When I get a result(row) using Ebean and the value of page column in my database has a specified value like 12, it works well. But if the value of page attribute is null in my database, the application will throw an exception:

Execution exception[[PersistenceException: Error loading on models.Book.page]]

I don't how to deal with this problem.

Herbivorous answered 23/8, 2013 at 14:19 Comment(1)
Could you use a try-catch?Impermissible
C
10

Use an Integer wrapper, that can be null, instead of int primitive:

public Integer page;

@Column(name="page")
public Integer getPage() {
    return page;
}

public void setPage(Integer page) {
    this.page = page;
}

Then you will be able to get\set nulls

Cornhusking answered 23/8, 2013 at 17:44 Comment(1)
You are my saviour, thanks a lot!Courtesy

© 2022 - 2024 — McMap. All rights reserved.