gpt4 book ai didi

java - spring cache redis : LazyInitializationException failed to lazily initialize a collection, 无法初始化代理 - 无 session

转载 作者:可可西里 更新时间:2023-11-01 11:24:29 27 4
gpt4 key购买 nike

使用redis作为spring cache,从redis获取对象时异常。json数据保存到redis如下

{"@class":"cc.giveme5.auth.RoleEntity","id":10001,"name":"member","description":"注册用户","type":1,"permissions":["org.hibernate.collection.internal.PersistentSet",[{"@class":"cc.giveme5.auth.PermissionEntity","id":10001,"name":"member:read","description":"注册用户访问许可"}]]}

## env
spring-boot: 2.0.2.RELEASE
spring-data-redis: 2.0.7.RELEASE
jackson: 2.9.5
hibernate: 5.0.1

Spring Boot 默认注册 OpenEntityManagerInViewInterceptor 以在 spring-boot(2.0.2.RELEAS) 中应用“Open EntityManager in View”模式。

实体

@Entity
@Table(name = "t_auth_role_m")
@JsonIgnoreProperties(ignoreUnknown = true)
public class RoleEntity implements Serializable
{
@Id
@TableGenerator(name = "UrRole_gen", table = "t_com_id_generator_r", pkColumnName = "seq_name", pkColumnValue = "UrRole_id", valueColumnName = "seq_value", allocationSize = Constants.SQE_ALLOCATION)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "UrRole_gen")
private Long id;

private String name;

private String description;

/**
* 角色对应许可
*/
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "t_auth_role_perm_r",
joinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "perm_id", referencedColumnName = "id") })
private Set<PermissionEntity> permissions;
}

@Entity
@Table(name = "t_auth_perm_m")
@JsonIgnoreProperties(ignoreUnknown=true)
public class PermissionEntity implements Serializable {

@Id
private Long id;

@Column(length = 32, name = "perm_name", nullable = false)
@Size(max = 32)
private String name;

@Column(length = 128, name = "perm_description", nullable = false)
@Size(max = 128)
private String description;

}

配置

@Configuration
@EnableCaching
public class CacheConfiguration {

@Autowired
private RedisConnectionFactory redisConnectionFactory;


@Bean
@ConfigurationProperties(prefix = "giveme5.cache")
public Properties4Cache cacheProperties(){
return new Properties4Cache();
}

@Bean
@Primary
public CompositeCacheManager cacheManager(){

CompositeCacheManager cacheManager = new CompositeCacheManager();

ArrayList<CacheManager> cacheManagers = new ArrayList<>();
//cacheManagers.add(simpleCacheManager());
cacheManagers.add(redisCacheManager(new redisObjectMapper(), redisConnectionFactory));
cacheManager.setCacheManagers(cacheManagers);
cacheManager.setFallbackToNoOpCache(true);
return cacheManager;
}


@Bean
public RedisCacheManager redisCacheManager(ObjectMapper objectMapper, RedisConnectionFactory redisConnectionFactory) {

RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(cacheProperties().getRedisCacheTtl()))
.computePrefixWith(cacheName -> cacheProperties().getName().concat(":").concat(cacheName).concat(":"))

.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)))

;

RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(cacheConfiguration)
.build();
cacheManager.setTransactionAware(true);
return cacheManager;
}



public class redisObjectMapper extends ObjectMapper {

public redisObjectMapper() {

super();

// 将类型序列化到属性json字符串中
this.enableDefaultTyping(DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);


this.registerModule(new Hibernate5Module().disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION));

// 对于找不到匹配属性的时候忽略报错
this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 不包含任何属性的bean也不报错
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);


}

}
}

服务&&存储库

@Service
@Transactional
public class RoleService {

@Autowired
private IRoleRepository roleRepository;

@Cacheable(cacheNames = "default", key = "'role.' + #p0")
public RoleEntity role(Long roleId) {
return roleRepository.getOne(roleId);
}

}



public interface IRoleRepository extends BaseRepository<RoleEntity, Long, BaseVO> {
}

异常

服务中发生错误:

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: failed to lazily initialize a collection, could not initialize proxy - no Session (through reference chain: cc.giveme5.auth.RoleEntity["permissions"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection, could not initialize proxy - no Session (through reference chain: cc.giveme5.auth.RoleEntity["permissions"])
at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:132) ~[spring-data-redis-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:110) ~[spring-data-redis-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.redis.serializer.DefaultRedisElementReader.read(DefaultRedisElementReader.java:50) ~[spring-data-redis-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.read(RedisSerializationContext.java:204)
...

Caused by: com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection, could not initialize proxy - no Session (through reference chain: cc.giveme5.auth.RoleEntity["permissions"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:391) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:351) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1704) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:290) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:189) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:130) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:193) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3079) ~[jackson-databind-2.9.5.jar:2.9.5]
at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:130) ~[spring-data-redis-2.0.7.RELEASE.jar:2.0.7.RELEASE]
... 96 common frames omitted
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:201) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:145) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:143) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:302) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:116) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromArray(AsArrayTypeDeserializer.java:53) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserializeWithType(CollectionDeserializer.java:314) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.9.5.jar:2.9.5]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288) ~[jackson-databind-2.9.5.jar:2.9.5]
... 106 common frames omitted

最佳答案

你好。当你调用它时,Hibernate 会初始化惰性属性,所以 permissions 不会在加载 RoleEntity 时初始化。hibernate 加载根对象后,第一次调用permissions 时,hibernate 从数据库中获取数据并加载permissions 属性。为了启动惰性属性,hibernate 需要一个有效的 session 。如果关闭 hibernate session ,则对象分离并且无法初始化惰性属性并引发 LazyInitializationException为了解决你可以使用:1- 设置获取类型加入。在一个选择语句中加载具有根对象的属性。2- 在关闭 session 之前,调用permissions 属性进行启动。3- 实现从 RoleEntity 对象加载 permissions 属性的方法。

这有用吗?

阿亚提

关于java - spring cache redis : LazyInitializationException failed to lazily initialize a collection, 无法初始化代理 - 无 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225063/

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