SpringDataJpa的@Query注解报错的解决

目录
  • SpringDataJpa @Query注解报错
  • SpringDataJpa @query注解使用原生代码报错

SpringDataJpa @Query注解报错

public interface TimeContentRepository extends JpaRepository<TimeContent,String> {
    @Query(value = "select id,user_id as userId,create_time as createTime " +
            "from time_content where create_time = ?1 and user_id = ?2")
    List<TimeContent> findOnDay(String create_time,String userId);
}

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: time_content is not mapped

注解中写的是HQL,所以查询的是对象,而不是表名

改为

public interface TimeContentRepository extends JpaRepository<TimeContent,String> {
    @Query(value = "select id,user_id as userId,create_time as createTime " +
            "from TimeContent where create_time = ?1 and user_id = ?2")
    List<TimeContent> findOnDay(String create_time,String userId);
}

Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode: 'user_id' {originalText=user_id}

同样的问题,查询的字段也是对象的成员,不是表的字段

SpringDataJpa @query注解使用原生代码报错

之前用过@query 原生代码的查询方式,正常加注解就可以使用,大概形式为:

 @Query(value="select * from table",nativeQuery=true)
   K_KC54 getK_KC54UsingOriginSQL(String aac001);

如上形式,完美解决本地查询问题。

但是,这是和往常一样使用@query 原生代码查询,程序报如下错误:

org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException: Cannot use native queries...

经过问题分析与网上查找原因发现问题:

@query 原生查询不能和分页查询的pageable一起使用。为解决这一问题,还想使用pageable分页功能。修改原生代码如下形式即可解决问题:

@Query(value="from S_TC70  aac001=?1 "
,countQuery="select count(1) from S_TC70  aac001=?1 ")
Page<S_TC70> getUseOriginS_TC70(String aac001,Pageable pageable);

顺利解决问题!以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 在JPA的@Query注解中使用limit条件(详解)

    在@Query注解注释的JPQL语句中写limit语句是会报错的 unexpected token :limit near line .... 解决方法是讲@Query注解中的limit语句去掉,然后传一个Pageable pageable=new PageRequest(offset,limit)进去 示例代码: controller import java.util.List; import org.springframework.beans.factory.annotation.Autow

  • 聊聊Spring data jpa @query使用原生SQl,需要注意的坑

    目录 Spring data jpa @Query 使用原生Sql的坑 根据代码来解说: 需要注意的方法有以下几点 SpringData JPA @Query动态SQL语句 思路 实现 Spring data jpa @Query 使用原生Sql的坑 根据代码来解说: @Query(value = "select bill.id_ as id, bill.created_date as date, bill.no, lawyer_case .case_no as caseNo, " +

  • 详解Spring Data JPA使用@Query注解(Using @Query)

    经过几天的折腾,终于到了学习一个重量级的查询方式上,使用@Query注解,使用注解有两种方式,一种是JPQL的SQL语言方式,一种是原生SQL的语言,略有区别,后者我们更熟悉一些.话不多说,看代码. 1.在CustomerRepository里添加 /** * 模糊匹配 * @param bauer * @return */ @Query("select c from Customer c where c.firstName=?1") Customer findByFirstName2

  • 使用JPA中@Query 注解实现update 操作方法(必看)

    使用JPA中@Query 注解实现update 操作,代码如下: @Transactional @Modifying(clearAutomatically = true) @Query(value = "update info p set p.status =?1 where p.id = ?2",nativeQuery = true)  int updateStatusById( String status,  String id); 备注: 1.更新info表下指定id的statu

  • SpringDataJpa的@Query注解报错的解决

    目录 SpringDataJpa @Query注解报错 SpringDataJpa @query注解使用原生代码报错 SpringDataJpa @Query注解报错 public interface TimeContentRepository extends JpaRepository<TimeContent,String> { @Query(value = "select id,user_id as userId,create_time as createTime "

  • Mybatis Mapper中多参数方法不使用@param注解报错的解决

    目录 问题描述 寻求解决方案 寻找原因 拓展延伸 在使用低版本的Mybatis的时候,Mapper中的方法如果有多个参数时需要使用@param注解,才能在对应xml的sql语句中使用参数名称获取传入方法的参数值,否则就会报错.本文结合自身在真实开发环境中使用IDEA开发时遇到的问题来共同探讨一下不使用@Param注解报错背后的原因以及解决方案. 问题描述 最近使用IDEA进行开发,项目使用SpringBoot+Mybatis3.4.6,同样的代码检出到本地IDEA后运行,在一个业务查询模块报错,

  • spring无法引入注解及import org.springframework.web.bind.annotation.*报错的解决

    本文主要介绍了spring无法引入注解及import org.springframework.web.bind.annotation.*报错的解决,具体如下: 如图所示,spring的注解报红,无法引入包,推测是spring-web和spring-webmvc这两个包的问题,去看pom.xml, 显示没有问题 进入maven库,两个包都存在 那么,问题在哪里呢? 扫了一眼project里面的External libraries,发现并没有spring-web和spring-webmvc,尝试手动

  • mybatis中使用InsertProvider注解报错解决全过程

    目录 使用InsertProvider注解报错解决 mybatis注解开发@InsertProvider Userprovider类 mapper的书写 使用InsertProvider注解报错解决 目前项目在使用mybatis,并且是使用注解的方式. 在使用InsertProvider注解的时候报了一下的错误: org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation.  

  • mybatis不加@Parm注解报错的解决方案

    我的idea版本2017.3.4,低版本貌似不会加上这个配置,idea高版本会 补充知识:Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题 对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数 对象的封装,例如查询对象条件basequery对象 <select id="getProductByProductQuer

  • spring cloud feign不支持@RequestBody+ RequestMethod.GET报错的解决方法

    1.问题梳理: 异常:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported 很明显是最终feign执行http请求时把这个方法认定为POST,但feign client中又定义了RequestMethod.GET 或 @GetMapping,冲突导致报错 那么为什么feign会认为这个方法是post呢? 源码追踪: 1.我们从feignClient注解

  • laravel执行php artisan migrate报错的解决方法

    报错一 $ php artisan migrate Illuminate\Database\QueryException : could not find driver (SQL: select * fr om information_schema.tables where table_schema = dev_oms and table_name = migra tions) at D:\WWW\dev_oms\vendor\laravel\framework\src\Illuminate\D

  • mybatis-plus3.4.0逻辑删除报错的解决

    特别注意!!! 无论数据库字段,实体类字段,还是平时取其它字段名字时候,或者方法这些名字,一定不能和关键字重名,因为这样可能出现一些让人无法看出的错误 在进行mybatis-plus逻辑删除字段时候,我在数据库定义了delete字段定义为int类型,默认值是0,然后又在实体类中定义了这个字段delete并且在实体类的这个字段上面添加了注解@TableLogic,并且在application.properties中开启.(我使用的是3.4.0) 根据官方文档,在3.4.0版本中只需要以上两个步骤即

  • @Accessors(chain = true)注解报错的解决方案

    如下所示: Cannot invoke setItemTitle(String) on the primitive type void 定义的实体类如下: @Data public static class RefundOrderItem implements Serializable { /** * 商品标题 */ @JsonProperty("item_title") private String itemTitle; /** * 数量 */ private BigDecimal

  • Vue3发送post请求出现400 Bad Request报错的解决办法

    查了一下网上资料,报400一般无非就是两种: 1. Bad Request:“错误的请求" 2. Invalid Hostname:"不存在的域名” 在这里我的报错是因为前端请求头的content-type和后端不一致. 一般后端默认的内容类型是 application/x-www-form-urlencoded,而axios默认的是 applecation/json. 但是也有例外,要根据后端的注解来区分我们要转换的类型. 根据上一篇笔记上说的: @RequestBody 用 con

随机推荐