Java 016
Data JPA Entity Issues
Try
HackMD
Java 016
·
Follow
Last edited by
Victor Liang
on
Oct 27, 2022
Linked with GitHub
Contributed by
Edit
Comments
Feedback
Log in to edit or delete your comments and be notified of replies.
Sign up
Already have an account? Log in
There is no comment
Select some text and then click Comment, or simply add a comment to this page from below to start a discussion.
Discard
Send
Entity 設計
雙向多對一
解決透過 JSON 格式取得物件時造成的無限迴圈問題
多方的參照物件使用 @JsonBackReference 註解
一方的參照物件使用 @JsonManagedReference 註解
多對一無法刪除
當使用 @ManyToOne,且 CascadeType = REFRESH 時,無法透過Many 方的 DELETE METHOD 刪除Many 方資料。
原因: JPA Entities 不建議使用 Lombok 的 @Data 註釋 ( 因沒有相對應的例外處理註釋 ),如一方的 @Data 的 toString() 沒有將 Many 方的 List 忽略,進而導致 Many 方刪除時執行到 One 方的 ToString 出錯,而無法正常刪除。
so1: 將整合的註釋 @Data 拆解為個別註釋,並於 Many 的 List 加上 @ToString.Exclude。
sol2: Fetch Type 改為 LAZY,另外 @JsonBackReference 不能使用於集合物件上 ( List, Set etc
…
),故若需要由 Many 方呈現 One 方資料,One 方須改使用 @JsonIgore。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up
Comment