Spring Data JPA @Query是否支持投影?(Spring Boot 1.5.10等版本)
Hey there! Let's break down your question about @Query support for projections in your specific Spring Data JPA version and the DATAJPA-885 issue you encountered.
First, the short answer: @Query does support projections in Spring Data JPA 1.11.10, but with limitations that are exactly what DATAJPA-885 addresses.
Here's the detailed breakdown:
Supported Projection Scenarios
Interface-based projections work reliably with@Queryas long as the fields you select in your JPQL match the getter methods of your projection interface. For example:// Projection Interface public interface MyEntityProjection { Long getId(); String getName(); } // Repository Method @Query("SELECT e.id, e.name FROM MyEntity e WHERE e.status = 'ACTIVE'") List<MyEntityProjection> findActiveEntitiesProjection();This setup will correctly map the query results to your projection interface.
DATAJPA-885 Limitations
The issue DATAJPA-885 highlights is that constructor-based projections and some advanced projection use cases (like combining@Querywith Specifications) don't work as expected in this version. You might encounter exceptions likeNo converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [your.projection.class]when trying these patterns.
Workarounds & Fixes:
- If you need interface-based projections: Stick to matching query selected fields with your projection's getters, and this should work without issues.
- If you need constructor-based projections: In this version, you'll have to either manually map query results (using a
ResultTransformeror post-processing the results) or upgrade to a newer Spring Data JPA version (2.0.x and above, which corresponds to Spring Boot 2.0+). DATAJPA-885 was resolved in later releases, fully supporting constructor projections with@Query. - Alternative: Use Spring Data's derived query methods with projections instead of
@Query—this pattern is fully stable in 1.11.10.
内容的提问来源于stack exchange,提问作者John Zhang

