当前位置: 首页 > 知识库问答 >
问题:

Hibernate-未能懒洋洋地初始化角色集合:无法初始化代理-没有会话

易俊驰
2023-03-14

我在更新Spring Boot应用程序中的一个实体时得到了这个错误。

@Entity
public class User {
...
@ManyToMany
    @JoinColumn(name="locationId")
    @JsonIgnore
    private List<Location> locations;
...
//getters and setters
}
 @Entity
    public class Coupon {

    .....
    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.LAZY)
        private List<Location> locations;
    ....
    //getters and setters
    }
List<Location> locations = new ArrayList<>();
locations.addAll(user.getLocations());
Coupon coupon = couponService.generateCoupons(CouponType.TIME   , contract.getType().getTimeCredit(), locations, couponName);
    userService updateUser(user);
@Override
    public Coupon generateCoupons(CouponType type, int value, List<Location> locations, String couponName) {
        Coupon coupon = new Coupon();
        coupon.setActive(true);
        coupon.setCode(generateRandomCouponCode());
        coupon.setCouponCount(0);
        coupon.setCouponLimit(1);
        coupon.setCouponName(couponName);
        LocalDateTime limit = LocalDateTime.now().plusYears(1);
        coupon.setDateLimit(Date.from(limit.atZone(ZoneId.systemDefault()).toInstant()));
        coupon.setLocations(locations);
        coupon.setStartDate(new Date());
        coupon.setType(type);
        coupon.setUserLimit(1);
        coupon.setValue(value);

        return saveCoupon(coupon);
    }

这是完整的堆栈跟踪

aused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: it.besmart.eshare.persistence.model.User.locations, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204)
    at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148)
    at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261)
    at org.hibernate.collection.internal.PersistentBag.addAll(PersistentBag.java:328)
    at it.besmart.eshare.service.CouponService.generateCoupons(CouponService.java:248)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy171.generateCoupons(Unknown Source)
    at it.besmart.eshare.scheduler.cronjobs.CheckContract.executeInternal(CheckContract.java:102)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    ... 1 common frames omitted

共有1个答案

何涵衍
2023-03-14

先生,您没有在这里指定提取类型Eager。默认情况下,它是懒惰的提取。但你急切地想去拿它。这里有一个例外。

         user. getLocation();//Failed to lazy initialize User. 
         // locations the Exception what we seen in console

只需像这样修改用户模型类

       @ManytoMany(fetch=FetchType.EAGER)
       @JoinColumn(name="locationId")
        @JsonIgnore
        private List<Location> locations;
 类似资料: