Whenever I query a Table letting Spring Data/Hibernate build the query 'findByLabelAndUserId' on my code, it always seems to take forever.
I test the query on the sql server and it returns the result instantaneously.
When I test the query on my project, it tooks more than 4 seconds to fetch 10 lines.
Here are the things I think to do : using @Query, but this is will take too long to change many methods. I added an all-argument Constructor but nothing changed. I changed the fetch method from eager to lazy but nothing changed
I tried to to setup this property in the jdbc url : sendStringParametersAsUnicode=false
It reduce time by 0.5 seconds but this is not enough.
This is my code: LeaveController:
@RequestMapping(value = "/leaves-management/leaves", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('view_MY_LEAVE')")
public List<LeaveDto> getAllByUser(HttpServletRequest httpRequest) {
String token = httpRequest.getHeader(tokenHeader);
String username = jwtTokenUtil.getUsernameFromToken(token);
User user = userService.findByUsername(username);
List<Request> listRequests = requestService.findByLabelAndUserId(LabelRequest.LEAVE, user.getId());
return listModelToListDtoWithStatus(listRequests);
}
the entity section:
public class Request extends AbstractAuditEntity {
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private LabelRequest label;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private Status status;
@ManyToOne(fetch = FetchType.LAZY)
private User user;
}
What can I do to reduce time building query using spring data/hibernate.
Adding sendStringParametersAsUnicode=false to the jdbc url drastically improved the response time from ~2000ms to 200ms. Cant believe this. Thanks a bunch for this answer.