【问题标题】:Play Framework 2.1 and Ebean: model Finder returns no dataPlay Framework 2.1 和 Ebean:模型查找器不返回数据
【发布时间】:2013-05-11 09:10:36
【问题描述】:

我已经为此绞尽脑汁好几个小时了。我有两个模型:

@Entity
@Table(name = "app_user")
public class AppUser extends Model {

    @Id
    Long id;

    …

    @Constraints.Required
    @Valid
    @OneToOne(cascade = CascadeType.ALL, optional = false)
    public LocationAddress address;

    @Valid
    @OneToOne(cascade = CascadeType.ALL, optional = true)
    public LocationAddress addressBilling;

    …

}

@Entity
@Table(name = "location_address")
public class LocationAddress extends Model {

    @Id
    Long id;

    @Constraints.Required
    @Constraints.MaxLength(TextSize.DEFAULT)
    @Column(length = TextSize.DEFAULT, nullable = false)
    public String street;

    @Constraints.MaxLength(TextSize.TINY)
    @Column(length = TextSize.TINY)
    public String streetNo;

    @ManyToOne(cascade = CascadeType.PERSIST, optional = false)
    public Country country;

    …

    @OneToOne(mappedBy = "address")
    public AppUser userAddress;

    @OneToOne(mappedBy = "addressBilling")
    public AppUser userAddressBilling;

    @OneToOne(mappedBy = "address")
    public AdvertisingLocation advertisingLocationAddress;

    // -- Queries

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

    public static List<LocationAddress> all() {
        return find.all();
    }

    public static LocationAddress findById(long id) {
        return find.byId(id);
    }

}

问题是LocationAddress.all() 什么都不返回,因此AppUser.findById(1).address.street 抛出EntityNotFoundException: Bean has been deleted - lazy loading failed。不用说,数据库表不是空的。

有趣的是,Ebean.find(LocationAddress.class).findRowCount() 返回 3(这是正确的)。

有人知道可能是什么问题吗?谢谢。

【问题讨论】:

    标签: java playframework playframework-2.1 ebean


    【解决方案1】:

    又过了几个小时,我终于找到了问题的根源。 LocationAddress 中的 @OneToOne 字段是不需要的,如果为 null,则不会返回实体。

    【讨论】:

    • 我不明白答案。你是如何解决问题的?我看到了同样的事情。
    • 如果是双向关系,那么要返回的对象,关系的两边都不能为空,就像这里的情况一样。我将其更改为单向关系,因此即使一侧为空也会返回对象。
    • 啊。您从班级中删除了所有 @OneToOne 字段,然后删除了 find.allworked。我现在明白了。但在我看来,更需要挖掘它。在您的情况下,您不需要它们。其他人可能需要它们。您可以发布您的数据库以便我重现此内容吗?
    【解决方案2】:

    我遇到过几次这个问题。 (也播放 1。)

    我解决这个问题的方法是将字段设为私有并用 getter/setter 包装它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多