我的提取型別有錯誤,我不知道如何修復它!如果可以,請幫助我。: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"])
…………………………………… ………………………………………………………………………………………………………………………………………………………………………… ..... 供閱讀。
uj5u.com熱心網友回復:
問題是當 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 fetching:
@Query(value = "SELECT u FROM User u JOIN FETCH u.reservations")
List<User> findAllUsers();
您可以在以下在線資源中閱讀有關此問題的更多資訊:https : //www.baeldung.com/hibernate-initialize-proxy-exception
uj5u.com熱心網友回復:
For@OneToMany和@ManyToManyhibernate 默認使用延遲獲取方法。即,當您要求它獲取父物體時,它根本不會加載您的子物體。所以你可以有兩種更簡單的方法來克服這個問題。
- 使用
fetch=FetchType.EAGER上Reservations。
只需在 Role 類中更新以下內容。
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Reservation> reservations;
但要注意性能問題。Reservation當您請求Role物體時,Hibernate 將一直加載物體。如果這不是您的應用程式的問題,您可以輕松地做到這一點。
- 使用
JOIN FETCH方法
限定@Query的使用方法join fetch和使用的是,代替預定的
find()方法。通過這種方式,您可以在要求加載父物體時詢問 hibernate 要加載哪些其他表。
@Query(value = "SELECT u FROM User u JOIN FETCH u.reservations")
List<User> findUsersWithReservations();
下面的文章有關于它的更多細節以及其他一些方法。
https://thorben-janssen.com/lazyinitializationexception/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368489.html
