EntityNotFoundException: Bean deleted - lazy loading failed

I'm taking my first steps with Play! Framework (v2.1-rc1) with Java, and now I am facing the first problem with ebean. I have a navigation object with ManyToOne's attitude towards myself. As soon as I try to access the header field in parentNavigation, I get the following error:

[EntityNotFoundException: Bean has been deleted - lazy loading failed]

As I found out, an error appears only if the parent navigation does not exist in the database. Should I get an empty navigation object in this case?

The essence of navigation:

package models;

import javax.persistence.*;
import play.db.ebean.*;

@Entity
public class Navigation extends Model {

    @Id
    public Long id;

    @Column(name="c_title")
    public String title;

    @Column(name="id_parent")
    public Long parentId;

    @ManyToOne()
    @JoinColumn(name="id_parent")
    public Navigation parentNavigation;

    public static Finder<Long,Navigation> find = new Finder<Long,Navigation>(
        Long.class, Navigation.class
    );
}

My actions in the controller:

public static Result index() {
    Navigation navigation = Navigation.find.byId(2L); // this one doesn't work, but the entry with ID 30 does
    return ok(views.html.app.index.render(navigation));
}

And my opinion:

@(navigation: Navigation)

@main("Welcome to Play 2.0") {

    This navigation: @navigation.title <br>
    Parent: @navigation.parentNavigation.title 

}
+3
source share
2 answers

, parent_id, 2 (), 2.

, , . , parent_id NULL parent_id, .

+6

, null, :

This navigation: @navigation.title <br>
Parent: @if(navigation.parentNavigation != null){@navigation.parentNavigation.title} else {This nav has no parent}

, else,

+1

Source: https://habr.com/ru/post/1614989/


All Articles