基本问题:为什么不总是实例化 @Embedded 对象?
有趣的观察是,如果 @Embedded 对象不包含基本数据类型(int、boolean ...)或之前没有被触及,Ebean 不会实例化这些对象。例子:
@Entity
public class Embedder {
// getNotAutoInstantiated() will return null if this field was not touched before
@Embedded
private NotAutoInstantiated notAutoInstantiated = new NotAutoInstantiated();
// getAutoInstantiated() will always return an instance!
@Embedded
private AutoInstantiated autoInstantiated = new AutoInstantiated();
}
@Embeddable
public class AutoInstantiated {
// theKey is why this embedded object is always instantiated
private int theKey;
private String field1;
}
@Embeddable
public class NotAutoInstantiated {
private String field2;
}