1

几个小时以来,我一直在努力解决这个问题。我有两个模型:

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

有人看到可能是什么问题吗?谢谢你。

4

2 回答 2

0

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

于 2013-05-12T12:04:06.697 回答
0

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

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

于 2013-09-10T14:46:09.700 回答