@Embedded对象如果没有基本数据类型字段,则不会自动实例化该对象
2022-09-03 12:53:37
基本问题:为什么@Embedded对象并不总是实例化?
有趣的观察结果是,Ebean不会实例化@Embedded对象,如果这些对象不包含基本数据类型(int,布尔值...)或者以前没有接触过。例:
@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;
}