如何解决Micronaut 数据问题懒得获取相关实体
我目前正在将 micronaut 数据合并到现有 micronaut 项目中(作为升级项目的一部分),并且在延迟获取相关集合(具有 OnetoMany
关系)方面遇到了一些问题。
我有以下实体结构:
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class Merchant implements Serializable {
@Id private String name;
private String displayName;
private String staticSiteAddress;
private String emailFromAddress;
private String emailFromName;
private String etmEmailFromAddress;
private String etmEmailFromName;
private String phone;
private String username;
@Embedded private PasswordHash password;
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OnetoMany(mappedBy = "merchant",fetch = FetchType.LAZY)
@Builder.Default
private Set<MerchantStore> stores = new HashSet<>();
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OnetoMany(mappedBy = "merchant",fetch = FetchType.LAZY)
@Builder.Default
private Set<CustomWebLink> customWebLinks = new HashSet<>();
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OnetoMany(mappedBy = "merchant",fetch = FetchType.LAZY)
@Builder.Default
private Set<LoyaltyPlan> loyaltyPlans = new HashSet<>();
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OnetoMany(mappedBy = "merchant",fetch = FetchType.LAZY)
@Builder.Default
private Set<MerchantPref> merchantPrefs = new HashSet<>();
}
手头上,我可以使用以下存储库方法毫无问题地获取第一级相关实体(即 merchantPrefs
):
@NonNull
@Override
@Cacheable("merchant")
@Join(value = "stores",type = Join.Type.LEFT_FETCH)
@Join(value = "customWebLinks",type = Join.Type.LEFT_FETCH)
@Join(value = "merchantPrefs",type = Join.Type.LEFT_FETCH)
@Join(value = "loyaltyPlans",type = Join.Type.LEFT_FETCH)
Optional<Merchant> findById(@NonNull @NotNull String s);
但是,当我尝试从该实体中获取最内层的集合(例如 awards
)时:
@Data
@Entity
public class LoyaltyPlan implements Serializable {
@EmbeddedId private LoyaltyPlanID id;
@ToString.Exclude
@EqualsAndHashCode.Exclude
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("merchant")
@JoinColumn(name = "merchant",nullable = false)
private Merchant merchant;
private String displayName;
private String emailFromName;
private String emailFromAddress;
private String uniqueId;
private boolean defaultPlan;
private float pointsFactor;
private boolean hasgiftCards;
private boolean hasLoyaltyCards;
private boolean usesName;
private boolean hasEmail;
private boolean hasCellPhone;
private boolean needsgiftCards;
private boolean needsLoyaltyCards;
private boolean needsName;
private boolean needsEmail;
private boolean needsCellPhone;
private int signupBonus;
private int closingAwardThreshold;
private int maxPointsPerOrder;
private int minPricePerOrder;
private int recentHistoryDays;
private int expiringCouponReminderDays;
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OnetoMany(mappedBy = "loyaltyPlan",fetch = FetchType.LAZY)
private List<LoyaltyPlanAward> awards;
}
我得到一个 LazyInitializationException
。
org.hibernate.LazyInitializationException: Failed to lazily initialize a collection of role: fts.marketing.entities.plan.LoyaltyPlan.awards,Could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:606)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:218)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149)
at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:387)
at java.base/java.lang.Iterable.forEach(Iterable.java:74)
at fts.marketing.TestController.doSomething(TestController.java:29)
at fts.marketing.$TestControllerDeFinition$Intercepted.$$access$$doSomething(UnkNown Source)
at fts.marketing.$TestControllerDeFinition$$exec1.invokeInternal(UnkNown Source)
at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:146)
at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:73)
at io.micronaut.transaction.interceptor.TransactionalInterceptor.intercept(TransactionalInterceptor.java:134)
at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:82)
at fts.marketing.$TestControllerDeFinition$Intercepted.doSomething(UnkNown Source)
at fts.marketing.$TestControllerDeFinition$$exec1.invokeInternal(UnkNown Source)
at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:146)
at io.micronaut.context.DefaultBeanContext$4.invoke(DefaultBeanContext.java:474)
at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:312)
at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:118)
at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildresultEmitter$10(RoutingInBoundHandler.java:1369)
at io.reactivex.internal.operators.flowable.FlowableDefer.subscribeActual(FlowableDefer.java:35)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.Flowable.subscribe(Flowable.java:14882)
at io.micronaut.http.server.context.ServerRequestContextFilter.lambda$doFilter$0(ServerRequestContextFilter.java:62)
at io.reactivex.internal.operators.flowable.FlowableFrompublisher.subscribeActual(FlowableFrompublisher.java:29)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.Flowable.subscribe(Flowable.java:14885)
at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildExecutableRoute$6(RoutingInBoundHandler.java:1074)
at io.micronaut.web.router.DefaultUriRouteMatch$1.execute(DefaultUriRouteMatch.java:80)
at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:118)
at io.micronaut.http.server.netty.RoutingInBoundHandler.handleRouteMatch(RoutingInBoundHandler.java:698)
at io.micronaut.http.server.netty.RoutingInBoundHandler.channelRead0(RoutingInBoundHandler.java:554)
at io.micronaut.http.server.netty.RoutingInBoundHandler.channelRead0(RoutingInBoundHandler.java:1
此外,这似乎是一个偶发问题,因为有时相同的代码似乎可以正常工作。我不太确定出了什么问题,我认为我已经正确地建模了这些关系,因为在迁移到 micronaut-data
之前这些关系很好。
我不知道我在这里做错了什么,为了解决这个问题,任何帮助将不胜感激。
解决方法
在 Micronaut 中,OpenSessionInView 的概念不存在。 当交易已经关闭时,您似乎正在尝试访问该关系。看看这个:https://micronaut-projects.github.io/micronaut-sql/latest/guide/index.html#_understanding_lazyinitializationexception
解决方案:
- 不要使用 Lazy。而是每次都使用存储库去数据库 你需要这份清单。
- 使用 EAGER。
- 在进行查询的方法中使用@Transactional。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。