多對一關系中的代理
假設Student對Team是多對一的關系,并且<many-to-one>采取的是默認的加載策略(也即proxy)。那么,在session中取得Student后,在session外通過Student取出Team會發生什么情況呢?分三種情況討論:
1.student記錄對應的team_id為null。注意:是為null,而不是為""。下面為student表情況:
id??????????????????? stuName????????????? age???????????????? team_id
123abc????????????tom?????????????????????24?????????????????? null
這個時候調用:
log.info(stu.getTeam())打印的是“null”。
2. student記錄對應的team_id有值,但這個值并沒有對應到任何的實體team,比如隨便給team_id設一個值“abcd123adfsdaf”。
id??????????????????? stuName????????????? age???????????????? team_id
123abc????????????tom?????????????????????24?????????????????? abcd123adfsdaf
這個時候調用:
log.info(stu.getTeam())
彈出異常:“could not initialize proxy - the owning Session was closed”
log.info(stu.getTeam().getClass())
打印的是“class com.model.Team$$EnhancerByCGLIB$$ee64f4f1”。
log.info(stu.getTeam().getId())
打印的是“abcd123adfsdaf”
log.info(stu.getTeam().getTeamName())
彈出異常:“could not initialize proxy - the owning Session was closed”
3. student記錄對應的team_id有值,而且這個值對應到了一個實體team。
id??????????????????? stuName????????????? age???????????????? team_id
123abc????????????tom?????????????????????24?????????????????? 402821f90cae6c2b010cae6c31f90001
這個時候調用:
log.info(stu.getTeam())
彈出異常:“could not initialize proxy - the owning Session was closed”
log.info(stu.getTeam().getClass())
打印的是“class com.model.Team$$EnhancerByCGLIB$$ee64f4f1”。
log.info(stu.getTeam().getId())
打印的是“402821f90cae6581010cae6588880001”
log.info(stu.getTeam().getTeamName())
彈出異常:“could not initialize proxy - the owning Session was closed”
從上可以看到第2和第三種情況的測試結果是完全一樣的,具體的原因比較簡單,在此就不再多說。
不過有的時候“多對一”的“一”這端可以為null,因此就可以用以下的語句判斷:
if(student.getTeam()==null)
???log.info(“team為null”);
else{
???Hibernate.initialize(student.getTeam());//這段代碼是偽碼,我只是想表達這個意思。
???log.info(student.getTeam().getTeamName());
}
?
1.student記錄對應的team_id為null。注意:是為null,而不是為""。下面為student表情況:
id??????????????????? stuName????????????? age???????????????? team_id
123abc????????????tom?????????????????????24?????????????????? null
這個時候調用:
log.info(stu.getTeam())打印的是“null”。
2. student記錄對應的team_id有值,但這個值并沒有對應到任何的實體team,比如隨便給team_id設一個值“abcd123adfsdaf”。
id??????????????????? stuName????????????? age???????????????? team_id
123abc????????????tom?????????????????????24?????????????????? abcd123adfsdaf
這個時候調用:
log.info(stu.getTeam())
彈出異常:“could not initialize proxy - the owning Session was closed”
log.info(stu.getTeam().getClass())
打印的是“class com.model.Team$$EnhancerByCGLIB$$ee64f4f1”。
log.info(stu.getTeam().getId())
打印的是“abcd123adfsdaf”
log.info(stu.getTeam().getTeamName())
彈出異常:“could not initialize proxy - the owning Session was closed”
3. student記錄對應的team_id有值,而且這個值對應到了一個實體team。
id??????????????????? stuName????????????? age???????????????? team_id
123abc????????????tom?????????????????????24?????????????????? 402821f90cae6c2b010cae6c31f90001
這個時候調用:
log.info(stu.getTeam())
彈出異常:“could not initialize proxy - the owning Session was closed”
log.info(stu.getTeam().getClass())
打印的是“class com.model.Team$$EnhancerByCGLIB$$ee64f4f1”。
log.info(stu.getTeam().getId())
打印的是“402821f90cae6581010cae6588880001”
log.info(stu.getTeam().getTeamName())
彈出異常:“could not initialize proxy - the owning Session was closed”
從上可以看到第2和第三種情況的測試結果是完全一樣的,具體的原因比較簡單,在此就不再多說。
不過有的時候“多對一”的“一”這端可以為null,因此就可以用以下的語句判斷:
if(student.getTeam()==null)
???log.info(“team為null”);
else{
???Hibernate.initialize(student.getTeam());//這段代碼是偽碼,我只是想表達這個意思。
???log.info(student.getTeam().getTeamName());
}
?