Spring Data JPA 1.11.x为何新增Tuple的Hibernate版本检查?
Why does Spring Data JPA 1.11.x add Hibernate version check for Tuple?
Great question—let’s break down the reasoning behind this check and how it ties to your current issue.
First, some context: The Hibernate version check in AbstractJpaQuery was introduced to handle behavioral changes in Tuple handling across different Hibernate versions, specifically the shifts between pre-5.0 and 5.x+ releases.
Here’s a detailed breakdown:
- Before Hibernate 5.0, Tuple was treated as a simple non-managed type by Spring Data JPA. The projection logic could directly map query results to Tuple instances without conflicts.
- Starting with Hibernate 5.0, the ORM overhauled how it handles non-managed return types like Tuple. Hibernate began taking more control over instantiation and type mapping for these types, which broke Spring Data JPA’s existing projection logic that assumed consistent handling across versions.
- The check snippet you referenced (
return returnedType.isProjecting() && !getMetamodel().isJpaManaged(returnedType.getReturnedType()) // ? HibernateUtils.isVersionOrBe...) serves two key purposes:- It identifies when the query is returning a projection (like Tuple) that isn’t a JPA-managed entity.
- It verifies if the current Hibernate version supports the updated Tuple handling logic that Spring Data JPA 1.11.x relies on. If the version is too old, it either falls back to a compatible processing path or blocks the operation to avoid runtime errors like type casting failures, null pointers, or incorrect result mapping.
In your specific case, you’re using Hibernate 5.0.12.Final—a transitional version in the Hibernate 5.x line. The version check logic in Spring Data JPA 1.11.x might be incorrectly flagging this version as incompatible, which is why your Tuple usage is failing.
Quick Workarounds to Try:
- Upgrade Hibernate to a later 5.1+ release (Spring Boot 1.5.x supports Hibernate 5.1, so this should be a safe upgrade without breaking other dependencies).
- If upgrading isn’t an option, bypass the projection logic by using a custom query that explicitly maps results to a Tuple via
EntityManagerdirectly, instead of relying on Spring Data JPA’s repository method projections.
内容的提问来源于stack exchange,提问作者bobby guo
相关产品推荐
相关产品推荐

