-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix it so count queries work properly for select new com.example.Dto(...) situations #1869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Andy Wilkinson commented Here's a further code snippet that was provided on the Spring Boot issue:
import com.jasamedika.medifirst2000.entity.LevelTingkat;
import com.jasamedika.medifirst2000.entity.vo.LevelTingkatId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository("LevelTingkatDao")
public interface LevelTingkatDao extends CrudRepository<LevelTingkat, LevelTingkatId> {
@Query(QfindAllList)
Page<Map<String, Object>> findAllList(
@Param("kdProfile") Integer kdProfile,
Pageable pageable);
@Query(QfindAllList + " and model.namaLevelTingkat like :namaLevelTingkat ")
Page<Map<String, Object>> findAllList(
@Param("kdProfile") Integer kdProfile,
@Param("namaLevelTingkat") String namaLevelTingkat,
Pageable pageable);
String QfindAllList = "select new map(model as levelTingkat) "
+ " from LevelTingkat model left join model.id id "
+ " where id.kdProfile=:kdProfile "
+ " and model.statusEnabled=true ";
} |
Jens Schauder commented
The JPA specification does not allow for aliases inside Please remove the alias and check if that helps. Note: It is quite normal the Spring Data executes an additional count-query in order to determine the total size of the paged result. If you want to avoid the count-query you may use a |
Jens Schauder commented Seems to be a user error |
Kyle Anderson commented I've just hit this issue myself and it appears to be a breaking change with how Spring Data JPA generates the count query if the following conditions are met:
Example:
The issue goes away if the constructor has more than 1 parameter. Example:
|
luelista commented I've encountered the same problem as Kyle, was this changed on purpose or it this a regression? |
Jens Schauder commented Reopening this since judging by the comments, this seems to be a real issue |
This issue is still persistent and slightly annoying, any news about it? |
Hi, is this problem fixed in the new JPA versions? |
This issue does not appears when the constructor has more than one parameter. As a provisional solution you can make a DTO constructor with one dummy parameter like this:
And then in the query you can do the following:
|
Having the DTO with more than one constructor parameter seems to solve the issue. |
This no longer seems to be a problem: class FirstNameDto {
private String firstname;
public FirstNameDto(String firstname) {
this.firstname = firstname;
}
} @Query("SELECT new org.springframework.data.jpa.repository.sample.FirstNameDto(u.firstname) from User u where u.firstname = :firstname")
List<FirstNameDto> findByNamedQueryWithSingleConstructor(@Param("firstname") String firstname); @Test
void executesNamedQueryWithSingleConstructorExpression() {
userRepository.findByNamedQueryWithSingleConstructor("Dave");
} This runs green on Spring Data JPA's |
Actually confirmed. There is an escape clause built up around This patch now properly assembles a count query and thus properly supports Page-based methods, whether the DTO has one argument or more. |
Backported to |
@gregturn thanks for the fix! These days we found a corner case that I believe still triggered this bug with one of our queries. This is a simplified version of what we had: @Query("SELECT DISTINCT NEW versionDTO(thing.version)"
+ " FROM Thing thing"
+ " WHERE thing.id = :id ORDER BY thing.version ASC")
List<VersionDTO> findVersion(@Param("id") UUID id); So when we wanted to paged it, just by + Page<VersionDTO> findVersion(@Param("id") UUID id, Pageable pageable);
- List<VersionDTO> findVersion(@Param("id") UUID id); We got + @Query("SELECT DISTINCT NEW versionDTO(thing.version)"
- @Query("SELECT DISTINCT new versionDTO(thing.version)" It's not a big deal but one can't guess it quickly by the error message. IMO, the escape clauses would need a |
Phil Webb opened DATAJPA-1598 and commented
Originally raised in spring-projects/spring-boot#17961 but this looks like a Spring Data issue. I'll ask the OP for more info.
—
After upgrading spring boot to 2.1.7 from 2.1.6, running application are failing due to the issue:
In new
CrudRepository
, it seem, has adding "count" to Query HQL, because present ofPageable
parameter.0 votes, 5 watchers
The text was updated successfully, but these errors were encountered: