gpt4 book ai didi

java - @Embedded 对象如果没有基本数据类型字段则不会自动实例化

转载 作者:搜寻专家 更新时间:2023-10-30 21:07:00 25 4
gpt4 key购买 nike

基本问题:为什么不总是实例化@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;
}

最佳答案

对于 Hibernate,您可能需要查看问题 HHH-7610 .

特别是,从 5.1 开始,有一个实验性功能可以改变这种行为。请注意,此功能存在已知问题,在稳定之前不应在生产中使用。 org.hibernate.cfg.AvailableSettings 的 Javadocs 对此进行了详细说明):

/**
* [EXPERIMENTAL] Enable instantiation of composite/embedded objects when all of its attribute values are {@code null}.
* The default (and historical) behavior is that a {@code null} reference will be used to represent the
* composite when all of its attributes are {@code null}
* <p/>
* This is an experimental feature that has known issues. It should not be used in production
* until it is stabilized. See Hibernate Jira issue HHH-11936 for details.
*
* @since 5.1
*/
String CREATE_EMPTY_COMPOSITES_ENABLED = "hibernate.create_empty_composites.enabled";

hibernate.create_empty_composites.enabled 属性设置为 true,瞧瞧!

关于java - @Embedded 对象如果没有基本数据类型字段则不会自动实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13606220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com