在使用Hibernate的Spring MVC應用程序中,連接ManyToMany關係中的兩個實體的列表未正確填充。這些實體是Encounter
和Provider
。 Encounter
實體有一個屬性providers
,它是一個Property
實體的列表,但沒有正確填充。有人可以幫我弄清楚如何解決我的代碼,以便實體開始正確填充?實體關係沒有正確填充
對於未能正確填充ArrayList中的控制器的方法中的代碼如下:
@RequestMapping("/calendar")
public String showCalendar(@RequestParam("day") String day, org.springframework.web.context.request.WebRequest webRequest, Model model) {
String pid = webRequest.getParameter("pid");
System.out.println("............ pid is: " + pid);
model.addAttribute("pid", pid);
LocalDate mydate;
if(day.equals("")) {
mydate = new LocalDate();
} else {
mydate = new LocalDate(day);
}
System.out.println("------------------------ in controller, day of month is: " + mydate.getDayOfMonth());
AppointmentCalendar calendar = new AppointmentCalendar(mydate); // Or whatever you do to create it
List<Encounter> encountersforday = (List<Encounter>) clinicService.getEncountersForDay(mydate);
model.addAttribute("calendar", calendar);
model.addAttribute("encountersforday", encountersforday);
System.out.println("number of encountersforday is: " + encountersforday.size());
List<LocalTime> myblocks = calendar.getBlocks();
int[][] filledblocks = new int[myblocks.size()][3];
for (Integer i=0; i < encountersforday.size(); i++) {
System.out.println("i, encounterid, patientid, first, last, " +
"numProviders, dateTime are: " + i + ", " +
encountersforday.get(i).getId() + ", " +
encountersforday.get(i).getPatient().getId() + ", " +
encountersforday.get(i).getPatient().getFirstName() + ", " +
encountersforday.get(i).getPatient().getLastName() + ", " +
encountersforday.get(i).getProviders().size() + ", " +
encountersforday.get(i).getDateTime());
for (Integer g=0; g < myblocks.size(); g++) {
filledblocks[g][0] = g;//blockid
if (myblocks.get(g).getHourOfDay() == encountersforday.get(i).getDateTime().getHourOfDay()) {
if (myblocks.get(g).getMinuteOfHour() == encountersforday.get(i).getDateTime().getMinuteOfHour()) {
int hours = myblocks.get(g).getHourOfDay();
int mins = myblocks.get(g).getMinuteOfHour();
System.out.println("found match at: "+hours+":"+mins);
filledblocks[g][1] = i+1;//encounterid
filledblocks[g][2] = 1;//segmentindex
}
}
}
}
return "appointments/calendar";
}
在前面的代碼輸出的System.out.println()
命令以下:
number of encountersforday is: 5
i, encounterid, patientid, first, last, numProviders, dateTime are: 0, 1, 5, Peter, McTavish, 0, 2014-01-10T12:00:00.000-08:00
found match at: 12:0
i, encounterid, patientid, first, last, numProviders, dateTime are: 1, 2, 4, Harold, Davis, 0, 2014-01-10T11:30:00.000-08:00
found match at: 11:30
i, encounterid, patientid, first, last, numProviders, dateTime are: 2, 3, 3, Eduardo, Rodriquez, 0, 2014-01-10T10:00:00.000-08:00
found match at: 10:0
i, encounterid, patientid, first, last, numProviders, dateTime are: 3, 4, 2, Morticia, Adams, 0, 2014-01-10T09:30:00.000-08:00
found match at: 9:30
i, encounterid, patientid, first, last, numProviders, dateTime are: 4, 5, 1, Speed, Racer, 1, 2014-01-10T13:30:00.000-08:00
found match at: 13:30
正如你可以看到從上面的輸出中,只有Encounter
對象中的一個具有大於0的numProviders
。儘管事實上Encounters
中的每一個都在基礎數據表中指定了Provider
,但這是真實的。
注意:爲了簡化本貼,我已將相關代碼上傳到文件共享網站。您可以點擊下面的鏈接在文件共享網站上查看代碼。
您可以閱讀在底層數據庫at this link中爲encounters
和providers
創建和填充數據表的代碼。
Encounter
實體的代碼是at this link。
Provider
實體的代碼是at this link。
BaseEntity
的代碼是at this link。
Person
實體的代碼是at this link。
+1並且感謝您花時間去捕捉深藏在代碼中的那個明顯的錯誤。 – CodeMed