java - Hibernate 不急切
问题描述
我有两个类,“Article”,其中包含对“SurchargeGroup”的@ManyToOne 引用,该引用指定了该文章的附加费。
@Entity
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(doNotUseGetters = true)
@Audited
public final class Article {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Getter(onMethod_ = {@Key(PermissionKey.ARTICLE_ID_READ)})
@Setter(onMethod_ = {@Key(PermissionKey.ARTICLE_ID_WRITE)})
private int id;
@JoinColumn(nullable = false)
@ManyToOne
@Getter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_SUPPLIER_READ)})
@Setter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_SUPPLIER_WRITE)})
private SurchargeGroup surchargeGroup;
}
另一个类“SurchargeGroup”包含一个父对象引用,如果未设置任何父级不提供附加费的情况,则可以将附加费继承到“SurchargeGroup”。
@Table
@Entity
@EqualsAndHashCode(doNotUseGetters = true)
@Audited
public class SurchargeGroup implements Serializable, Cloneable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
@Getter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_ID_READ)})
@Setter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_ID_WRITE)})
private int id;
@Column
@Setter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_SURCHARGE_WRITE)})
private Double surcharge;
@Column
@Getter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_NAME_READ)})
@Setter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_NAME_WRITE)})
private String name;
@JoinColumn
@ManyToOne(fetch = FetchType.EAGER)
@Getter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_SUPPLIER_READ)})
@Setter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_SUPPLIER_WRITE)})
private SurchargeGroup parent;
public double getSurcharge() {
if (surcharge == null) {
return parent == null
? supplier == null
? Setting.SURCHARGE_DEFAULT.getDoubleValue()
: supplier.getDefaultSurcharge()
: parent.getSurcharge();
} else return surcharge;
}
@JoinColumn
@ManyToOne
@Getter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_SUPPLIER_READ)})
@Setter(onMethod_ = {@Key(PermissionKey.SURCHARGE_TABLE_SUPPLIER_WRITE)})
private Supplier supplier;
}
我现在的问题是,如果我调用“getSurcharge()”方法,我会得到这个异常,我无法向自己解释,因为我将附加费组标记为急切
Exception in thread "AWT-EventQueue-0" org.hibernate.LazyInitializationException: could not initialize proxy [kernbeisser.DBEntities.SurchargeGroup#1046] - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:172)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309)
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)
at kernbeisser.DBEntities.SurchargeGroup$HibernateProxy$cdTAuBkS.getSurcharge(Unknown Source)
我问自己这是否可能是由 @Audited 注释引起的?有任何想法吗?非常感谢!
注意:@Key 注释对这种情况没有影响。
解决方案
Hibernate 需要在某个时候停止急切地获取关联,否则它需要无限次地加入SurchargeGroup
实体(因为它引用了自身)。
hibernate.max_fetch_depth
可以使用该属性在应用程序范围内控制这些提取的深度。
推荐阅读
- azure - 当我的虚拟机磁盘空间不足时如何创建天蓝色监视器警报
- javascript - 防止子执行附加到容器的函数
- python - 字典 KeyNotFound
- django - Django重定向在使用反向时将元组附加到URL
- typescript - TypeScript:意外行为的关键
- android - Capacitor Plugin开发如何重新安装以调试/测试更改
- c# - 控制器中的 Asp.Net MVC 5 ModelState 具有索引、添加、编辑
- autodesk-forge - 在长链接上正确使用 addProperty()
- php - 无法使用 curl 在 cron 作业中包含 wp-load.php
- javascript - 如何一次定位多个数据属性