0

I am using spring boot and hibernate. as cache i am using redis and GenericJackson2JsonRedisSerializer.

When I try to retrieve my entity from cache iI am getting:

Failure getting from cache: user_entity. Could not read JSON: failed to lazily initialize a collection: could not initialize proxy - no Session (through reference chain: com.avid.identity.service.entity.User["identities"]

I was trying to change Lazy on Eager, it did not help. Please see entity below.

@Getter
@Setter
@ToString(of = {"id"})
@EqualsAndHashCode(of = {"id"})
@Entity
@NoArgsConstructor
@Table(name = "user", schema = "identity")
public class User {

@Id
@GeneratedValue(generator = "uuid4")
@GenericGenerator(name = "UUID", strategy = "uuid4")
@Column(columnDefinition = "CHAR(36)")
private UUID id;

@Fetch(FetchMode.JOIN)
@JoinColumn(name = "last_authentication_id", referencedColumnName = "id")
@OneToOne(fetch = FetchType.LAZY, orphanRemoval = true)
private AuthenticationEvent lastAuthentication;

@Fetch(FetchMode.JOIN)
@JoinTable(name = "user_identities", joinColumns = {
        @JoinColumn(name = "user_id")}, inverseJoinColumns = {
        @JoinColumn(name = "user_identity_id")})
@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true)
private List<Identity> identities;

@Fetch(FetchMode.JOIN)
@JoinTable(name = "user_roles", joinColumns = {
        @JoinColumn(name = "user_id")}, inverseJoinColumns = {
        @JoinColumn(name = "role_id")})
@OneToMany(fetch = FetchType.LAZY)
private List<Role> roles;

@Fetch(FetchMode.JOIN)
@JoinColumn(name = "tenant_id", referencedColumnName = "id")
@OneToOne(fetch = FetchType.LAZY)
private Tenant tenant;

Please help. Thank You

  • Does this answer your question? [Could not write JSON: failed to lazily initialize a collection of role](https://stackoverflow.com/questions/48117059/could-not-write-json-failed-to-lazily-initialize-a-collection-of-role) – Pirate Jun 06 '23 at 12:49
  • unfortunately no, in the link you mention it's writing to JSON while i have problems with retrieving data from redis cache (reading) – Денис Анд Jun 08 '23 at 09:42

0 Answers0