Udemy Course Spring JPA 20
由於Student與Passport為一對一對應關係
所以也可以在Passport內加入private Student student;
執行DemoApplication可至http://localhost:8090/h2-console看到:
在passport內的student_id欄位中為null
因為我們並沒有在data.sql內給定值,但是我們更想要看到自動產生值
在此之前我們必須決定table間的主從關係
術語為:owning side relationship
現在我們先決定以student為主
student為owning side relationship,意即student擁有passport id
我們希望看到student內看到passport id
但是我們不希望看到passport內出現student id(不然看太多重複的欄位)
所以我們再次改寫passport
再次執行DemoApplication得
我們可以看到student有passport id但是passport內沒有student id
接著我們想從取得passport資訊時順便將student資訊一起取得
在StudentRepositoryTest內新增retrievePassportAndAssociatedStudent
測執行該Method會看到
程式先找完passport再找student
這個關係稱為: bidirectional(雙向的) relationship