Search code examples
springpostgresqlspring-data-jpapaginationentitymanager

Pagination of an complex object into DTO. JPA @Query


I have the following query to take some data regarding two entities in the same time and I receive an error.

@Query(value = "select new base.models.HRTableEntity( yr.user.gid,  yr.user.id,  yr.user.lastName || ' ' || yr.user.firstName,  yr.user.position,  yr.user.created,yr.genericField1,yr.genericField2) from YearlyReview yr where yr.year = :yr and yr.user.realDepartment = :dep and yr.user.city = :ct",
            countQuery = "select count(yr.id) from YearlyReview yr where yr.year = :yr and yr.user.realDepartment = :dep and yr.user.city = :ct",
            nativeQuery = false)
    Page<HRTableEntity> getAllTableEntity(Pageable pageRequest, @Param("yr") int year, @Param("dep") String department, @Param("ct") String location);

I call this cunction with default Sort (gid: ASC) and receive the following error

org.hibernate.QueryException: could not resolve property: gid of: base.entities.YearlyReview

Repo interface:

public interface PageableYearlyReview extends CrudRepository<YearlyReview, UUID>

Yearly review have a member (user) of type ApplicationUser and I want to put information into DTO from yr.user.gid into HRTableEntry.gid.

What is the right way to do that ?

EDIT: function call:

crunRepoYearTable.getAllTableEntity(PageRequest.of(pageNo - 1, pageSize, sort), year, realDepartment, user.getCity())

sort building

sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortedField).ascending() : Sort.by(sortedField).descending();

Entity structure:

public class YearlyReview {
    @Id
    private UUID id;
    private int year;
    @OneToOne
    private ApplicationUser user;
....
}
public class ApplicationUser {
    @Id
    private String id;
    private String gid;
.....
}

Solution

  • Problem solved:

    for sort by gid need to send from the fronted like this user.gid. User is required to refer at AppUser and gid to access information. And change interface like this public interface PageableYearlyReview extends CrudRepository<ApplicationUser, String>