实体关系没有正确填充

时间:2014-02-25 00:38:29

标签: java spring hibernate

在使用Hibernate的Spring MVC应用程序中,在ManyToMany关系中连接两个实体的列表未正确填充。实体为EncounterProviderEncounter实体有一个属性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中为encountersproviders创建和填充数据表的代码。

Encounter实体的代码为at this link

Provider实体的代码是at this link

BaseEntity的代码是at this link

Person实体的代码是at this link

1 个答案:

答案 0 :(得分:1)

providerEncounterJunction表中的每个插入都使用相同的主键。我猜他们只是简单地覆盖了对方,你只在该表中有一行。