一尘不染

实体关系无法正确填充

hibernate

在使用Hibernate的Spring
MVC应用程序中,无法将在ManyToMany关系中连接两个实体的列表正确填充。实体是EncounterProvider。该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。尽管事实是每一个EncountersProvider在基础数据表中指定了一个事实,但这是正确的。

注意:为简化此发布,我已将相关代码上传到文件共享站点。 您可以通过单击下面的链接在文件共享站点上查看代码。

你可以阅读的代码,用于创建和填充数据表encountersproviders基础数据库,在这个环节

Encounter实体的代码位于此链接上

Provider实体的代码位于此链接上

对于该代码BaseEntity在这个环节

Person实体的代码位于此链接上


阅读 244

收藏
2020-06-20

共1个答案

一尘不染

providerEncounterJunction表中的每个插入都使用相同的主键。我猜他们只是在相互覆盖,而您在该表中实际上只有一行。

2020-06-20