無法寫入 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"])

…………………………………… ………………………………………………………………………………………………………………………………………………………………………… ..... 供閱讀。

The_Java_Guy

For@OneToMany@ManyToManyhibernate 默認使用延遲獲取方法。即,當您要求它獲取父實體時,它根本不會加載您的子實體。所以你可以有兩種更簡單的方法來克服這個問題。

  1. 使用fetch=FetchType.EAGERReservations

只需在 Role 類中更新以下內容。

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

但要注意性能問題。Reservation當您請求Role實體時,Hibernate 將一直加載實體。如果這不是您的應用程序的問題,您可以輕鬆地做到這一點。

  1. 使用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/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

無法寫入 JSON:無法通過反射設置字段值 [t]

Cloud Function 無法解析 JSON 並從 Webhook 寫入 Cloud Firestore

“未能延迟初始化角色集合”

ElementCollection和“未能延迟初始化角色的集合”异常

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

異常 'JSON 寫入中的無效類型

無法將值寫入特定單元格

休眠:LazyInitializationException异常:未能初始化懒洋洋角色的集合。无法初始化代理 - 没有会话

转换导致发生错误:未能延迟初始化角色集合:无法初始化代理 - 没有会话

無法配置格式化程序“json”:無法解析“app_name.utils.logging.JSONFormatter”:無法導入名稱“Celery”

JSON 寫入崩潰中的另一個無效頂級類型

Kubernetes:無法在初始化容器內 VolumeMount emptydir

初始化前無法訪問“NgbNavLink”

初始化前無法訪問“VariableName”

支持不可變集合的集合初始化語法 - 方法的 init 修飾符?

Azure网站抱怨角色环境。未能初始化

Google Cloud Function 無法(寫入)/(創建)bigquery 表

無法使用openpyxl將數據寫入python中的xlsx文件

Java SSL 異常:無法創建 StAX 讀取器或寫入器

無法將輸入從 JTextField 存儲到文件編寫器

無法寫入 Snowflake 表但沒有返回錯誤

在 nodejs 中,bigquery 無法查詢 Parquet 寫入 gcs

無法使用 XSL 在 CSV 文件中寫入 XML 數據

導入錯誤:無法從部分初始化的模塊 'affiliate.models' 導入名稱 'Affiliate'(很可能是由於循環導入)

導入錯誤:無法從部分初始化的模塊 'ArabicOcr' 導入名稱 'arabicocr'(很可能是由於循環導入)

鍵盤輸入無重複延遲

無法使用 Android 將數據寫入 Firebase 數據庫時如何獲取錯誤消息

異步/等待:初始化前無法訪問變量(javscript)

無法初始化類 sun.util.calendar.ZoneInfoFile intellij