我一直在为此敲打几个小时。我有两个模型:
@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(这是正确的)。
有人看到可能出现的问题吗?谢谢。
答案 0 :(得分:0)
经过多个小时后,我终于找到了问题的根源。 @OneToOne
中的LocationAddress
字段不需要,如果为null,则不会返回实体。
答案 1 :(得分:0)
我遇到过这个问题几次。 (也玩1。)
我解决这个问题的方法是将字段设为私有,并使用getter / setter将其包装起来。