EntityNotFoundException:Bean已被删除-延迟加载失败 [英] EntityNotFoundException: Bean has been deleted - lazy loading failed

查看:248
本文介绍了EntityNotFoundException:Bean已被删除-延迟加载失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Play迈出第一步! Java框架(v2.1-rc1),现在我遇到了第一个关于ebean的问题.我有一个与自身具有ManyToOne关系的导航实体.尝试访问parentNavigation中的title字段时,出现以下错误:

I'm making my first steps with the Play! Framework (v2.1-rc1) with Java and now I've faced my first problem with ebean. I have a navigation entity with a ManyToOne relationship to itself. As soon as I try to access the title field in a parentNavigation, I get the following error:

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

我发现,仅在数据库中不存在父导航的情况下才会出现该错误.在这种情况下,我不应该收到一个空的导航对象吗?

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

导航实体:

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
    );
}

我在控制器中的动作:

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));
}

我的观点:

@(navigation: Navigation)

@main("Welcome to Play 2.0") {

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

}

推荐答案

如果我理解正确,那么您会有一行其parent_id列包含2的行(例如),但是没有ID为在表格中.

If I understand correctly, you have a row with its parent_id column containing 2 (for example), but there is no row with ID 2 in the table.

如果是这样,那么获得异常是正常的.通过将所有不存在的parent_id都设置为NULL来清理数据,并在parent_id列中添加外键约束,以使这种情况不再发生.

If so, then it's normal to get an exception. Clean your data by setting all those inexistent parent_id to NULL, and add a foreign key constraint to the parent_id column so that this situation never happens anymore.

这篇关于EntityNotFoundException:Bean已被删除-延迟加载失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆