有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在spring中记录SQL查询,隐藏的SQL请求比预期的要多

因此,我在spring中启用了sql日志记录来查看执行了哪些sql查询,因为有些请求花费的时间比预期的要长

应用程序。yml:

logging:
  level:
    org:
      hibernate:
        SQL: DEBUG
        type:
          descriptor:
            sql:
              BasicBinder: TRACE

然后,我对以下API方法执行了API请求:

  @GetMapping(params = "employeeId")
  public ResponseEntity<List<DailyEntry>> getDailyEntriesFromEmployeeId(@RequestParam Long employeeId) {
    return ResponseEntity.ok(dailyEntryService.getDailyEntriesFromEmployeeId(employeeId));
  }

这将调用此服务方法:

  List<DailyEntry> getDailyEntriesFromEmployeeId(Long employeeId) {
    List<DailyEntry> dailyEntries = dailyEntryRepository.findByEmployeeId(employeeId);
    return dailyEntries;
  }

findByEmployee()在CRUDREposition中定义:

List<DailyEntry> findByEmployeeId(@Param("id") long id);

DailyEntry实体如下所示:

@Data
@Entity
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DailyEntry {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private LocalDate date;
  private LocalTime startTime;
  private LocalTime endTime;
  // more unimportant fields

  @ManyToOne
  private Project project;

  @ManyToOne
  private Employee employee;
}

因此,通过调用该API方法,我希望看到一个SQL方法正在执行,即dailyEntryRepository.findByEmployeeId(employeeId);调用的SQL方法

但最重要的是,我实际上看到了列表中每个DailyEntry的以下SQL请求:

2019-12-09 11:54:13.942 DEBUG 1400 --- [nio-8080-exec-4] org.hibernate.SQL                        : select project0_.id as id1_9_0_, project0_.archived_date as archived2_9_0_, project0_.budget as budget3_9_0_, project0_.creation_date as creation4_9_0_, project0_.customer_id as customer8_9_0_, project0_.default_daily_entry_settings_id as default_9_9_0_, project0_.description as descript5_9_0_, project0_.is_archived as is_archi6_9_0_, project0_.name as name7_9_0_, customer1_.id as id1_0_1_, customer1_.address as address2_0_1_, customer1_.name as name3_0_1_, customer1_.smallest_time_unit as smallest4_0_1_, customer1_.smallest_time_unit_enabled as smallest5_0_1_, customer1_.supplier_number as supplier6_0_1_, defaultdai2_.id as id1_2_2_, defaultdai2_.end_date_hour as end_date2_2_2_, defaultdai2_.end_date_minute as end_date3_2_2_, defaultdai2_.pause as pause4_2_2_, defaultdai2_.performance_record as performa5_2_2_, defaultdai2_.start_date_hour as start_da6_2_2_, defaultdai2_.start_date_minute as start_da7_2_2_ from project project0_ inner join customer customer1_ on project0_.customer_id=customer1_.id left outer join default_daily_entry_settings defaultdai2_ on project0_.default_daily_entry_settings_id=defaultdai2_.id where project0_.id=?
2019-12-09 11:54:13.942 TRACE 1400 --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [8]

然后,对列表中的每个DailyEntry都有这个请求:

2019-12-09 11:57:18.142 DEBUG 1400 --- [nio-8080-exec-8] org.hibernate.SQL                        : select employees0_.project_id as project_1_4_0_, employees0_.employee_id as employee2_4_0_, employee1_.id as id1_3_1_, employee1_.address as address2_3_1_, employee1_.email as email3_3_1_, employee1_.enabled as enabled4_3_1_, employee1_.first_name as first_na5_3_1_, employee1_.last_name as last_nam6_3_1_, employee1_.weekly_hours as weekly_h7_3_1_, employee1_.weekly_hours_enabled as weekly_h8_3_1_ from employee_projects employees0_ inner join employee employee1_ on employees0_.employee_id=employee1_.id where employees0_.project_id=?
2019-12-09 11:57:18.142 TRACE 1400 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [8]

这里到底发生了什么,为什么?他通过找到项目的id来获得该项目的id。。。他已有的身份证

这就是Project实体,如果这很重要的话:

@Data
@Entity
@ToString(exclude = {"employees"})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Project {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  @Column(unique = true)
  private String name;

  private Integer budget;

  private String description;

  private Boolean isArchived;

  private LocalDate archivedDate;

  private LocalDate creationDate;

  @NotNull
  @ManyToOne
  private Customer customer;

  @OneToOne(cascade = CascadeType.ALL)
  private DefaultDailyEntrySettings defaultDailyEntrySettings;

  @ManyToMany
  @JoinTable(
          name = "employee_projects",
          joinColumns = @JoinColumn(name = "project_id"),
          inverseJoinColumns = @JoinColumn(name = "employee_id")
  )
  private List<Employee> employees;

  @Transient
  private Long usedBudget;

  @Transient
  private Integer notificationCount;

  @Transient
  private Boolean isInactive;

  @Transient
  private Integer inactiveSince;
}

共 (0) 个答案