无法写入JSON:未能延迟初始化角色的集合:



我在获取类型上有一个错误,我不知道如何修复它!如果可以的话,请帮帮我D.使用java 8

命令行运行程序:

@Autowired CustomTableRepository tr;
@Autowired UserRepository ur;
@Autowired RoleRepository rr;
@Bean
CommandLineRunner commandLineRunner() {
return args -> {
tr.save(new CustomTable(null, true, null));
tr.save(new CustomTable(null, true, null));
tr.save(new CustomTable(null, true, null));
tr.save(new CustomTable(null, true, null));
tr.save(new CustomTable(null, true, null));
ur.save(new User(null, "cpthermes", "thanatos", 123987456l, 3123231l, null,null));
ur.save(new User(null, "moni1008", "milky", 123987456l, 31232131l, null, null));
ur.save(new User(null, "mario", "zoro123", 1231231l, 32123l, null, null));

};
}

模型类:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Reservation {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private Boolean accepted;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@OneToOne
@JoinColumn(name = "table_id")
private CustomTable table;
private LocalTime time;
}

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String username;
private String password;
private Long number;
private Long balance;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Reservation> reservations;
@ManyToMany
private Collection<Role> roles;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Role {
@Id
@GeneratedValue
private Long id;
private String type;
}

错误:

Could not write JSON: failed to lazily initialize a collection of role: com.mile.pc.mile.restoraunt.app.model.User.reservations, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.mile.pc.mile.restoraunt.app.model.User.reservations, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.mile.pc.mile.restoraunt.app.model.User["reservations"])
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.mile.pc.mile.restoraunt.app.model.User.reservations, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.mile.pc.mile.restoraunt.app.model.User.reservations, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.mile.pc.mile.restoraunt.app.model.User["reservations"])

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。Ty用于阅读。

对于@OneToMany@ManyToMany,hibernate默认使用延迟获取方法。即,当您要求它获取父实体时,它根本不会加载您的子实体。所以你可以用两种更简单的方法来克服这个问题。

  1. Reservations上使用fetch=FetchType.EAGER

只需在下面的角色类中更新即可。

@OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Reservation> reservations;

但要注意性能问题。当您请求Role实体时,Hibernate将一直加载Reservation实体。如果你的应用程序不关心这一点,你可以很容易地使用它。

  1. 使用JOIN FETCH方法

使用join fetch定义@Query方法,并使用该方法而不是预定义的CCD_ 10方法。通过这种方式,您可以询问hibernate要加载哪些其他表每当您要求加载父实体时。

@Query(value = "SELECT u FROM User u JOIN FETCH u.reservations")
List<User> findUsersWithReservations();

下面的文章有关于它的更多细节以及一些其他方法。

https://thorben-janssen.com/lazyinitializationexception/

问题是,当Spring尝试将实体转换为JSON时,Hibernate无法从数据库中检索延迟加载的reservations。你有几种可能性来解决这个问题:

  • 使用FetchType.EAGER策略
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String username;
private String password;
private Long number;
private Long balance;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Reservation> reservations;
@ManyToMany
private Collection<Role> roles;
}
  • 在自定义Repository方法中使用Join获取:
@Query(value = "SELECT u FROM User u JOIN FETCH u.reservations")
List<User> findAllUsers();

您可以在以下在线资源中阅读有关此问题的更多信息:https://www.baeldung.com/hibernate-initialize-proxy-exception

最新更新