Play Framework 2.1和Ebean:模型Finder不返回任何数据

时间:2013-05-11 09:10:37

标签: java playframework playframework-2.1 ebean

我一直在为此敲打几个小时。我有两个模型:

@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(这是正确的)。

有人看到可能出现的问题吗?谢谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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