JPA原生查询抛出InvalidDataAccessResourceUsageException问题排查
问题分析与解决方案
你遇到的InvalidDataAccessResourceUsageException异常,本质是JPA无法将查询返回的部分字段映射到完整的Teabag实体类。我之前也碰到过类似的坑,给你拆解一下问题原因和解决办法:
为什么SQL能在数据库运行,但程序报错?
控制台输出的SQL能正常执行是因为数据库只需要返回你指定的3个字段即可,但JPA要把结果转换成Teabag实体对象时,需要填充实体中所有被持久化的属性(比如id、user_id这些你没查询的字段)。缺少这些字段的值,JPA无法完成实体映射,就会抛出资源使用异常。
解决办法(三种可选方案)
方案1:使用接口投影(最简洁)
定义一个只包含你需要的字段的接口,JPA会自动生成代理类来映射查询结果:
// 定义投影接口 public interface TeabagProjection { String getPost(); String getStatus(); LocalDateTime getExpires(); } // 修改仓库接口的返回类型 public interface TeabagRepository extends CrudRepository<Teabag, Long> { @Query(value = "SELECT t.post, t.status, t.expires FROM teabags t WHERE t.status = 'hot' AND t.user_id = ?1", nativeQuery = true) TeabagProjection findTea(Long id); }
方案2:使用DTO构造函数投影
创建一个专门的DTO类,通过构造函数接收查询的字段,适合需要对结果做额外处理的场景:
// 定义DTO类 public class TeabagSummary { private String post; private String status; private LocalDateTime expires; // 注意构造函数的参数顺序要和查询字段顺序一致 public TeabagSummary(String post, String status, LocalDateTime expires) { this.post = post; this.status = status; this.expires = expires; } // 按需添加getter方法 public String getPost() { return post; } public String getStatus() { return status; } public LocalDateTime getExpires() { return expires; } } // 修改仓库接口(建议改用JPQL,原生SQL需要额外配置映射) public interface TeabagRepository extends CrudRepository<Teabag, Long> { @Query(value = "SELECT new com.yourpackage.TeabagSummary(t.post, t.status, t.expires) FROM Teabag t WHERE t.status = 'hot' AND t.userId = ?1") TeabagSummary findTea(Long id); }
注:如果坚持用原生SQL,需要配合@SqlResultSetMapping来指定结果到DTO的映射,步骤会更繁琐,推荐用JPQL的方式。
方案3:查询完整实体(如果需要完整对象)
如果你实际上需要完整的Teabag对象,修改查询语句返回所有字段即可:
public interface TeabagRepository extends CrudRepository<Teabag, Long> { // 原生SQL写法 @Query(value = "SELECT * FROM teabags t WHERE t.status = 'hot' AND t.user_id = ?1", nativeQuery = true) Teabag findTea(Long id); // 或者更推荐的JPQL写法 // @Query("SELECT t FROM Teabag t WHERE t.status = 'hot' AND t.userId = ?1") // Teabag findTea(Long id); }
内容的提问来源于stack exchange,提问作者el_pup_le
相关产品推荐
相关产品推荐

