解决Spring Spring Data JPA 错误: Page 1 of 1 containing UNKNOWN instances

SpringBoot 整合 Spring-Data-JPA 的时候出现此问题:

前端 js 代码

$(function () {var searchText = $('.search').find('input').val()var columns = [];columns.push({title: '分类',field: 'category',align: 'center',valign: 'middle',formatter: function (value, row, index) {return value}}, {title: '美图',field: 'url',align: 'center',valign: 'middle',formatter: function (value, row, index) {return "![](" + value + ")"}})$.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales['zh-CN']);$('#meituFavoriteTable').bootstrapTable({url: 'meituSearchFavoriteJson',sidePagination: "server",queryParamsType: 'page,size',contentType: "application/x-www-form-urlencoded",method: 'get',striped: false,     //是否显示行间隔色cache: false,      //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)pagination: true,  //是否显示分页(*)paginationLoop: true,paginationHAlign: 'right', //right, leftpaginationVAlign: 'bottom', //bottom, top, bothpaginationDetailHAlign: 'left', //right, leftpaginationPreText: ' 上一页',paginationNextText: '下一页',search: true,searchText: searchText,searchTimeOut: 500,searchAlign: 'right',searchOnEnterKey: false,trimOnSearch: true,sortable: true,    //是否启用排序sortOrder: "desc",   //排序方式sortName: "id",pageNumber: 1,     //初始化加载第一页,默认第一页pageSize: 10,      //每页的记录行数(*)pageList: [5, 10, 20, 50, 100], // 可选的每页数据totalField: 'totalPages',dataField: 'content', //后端 json 对应的表格数据 keycolumns: columns,queryParams: function (params) {return {size: params.pageSize,page: params.pageNumber,sortName: params.sortName,sortOrder: params.sortOrder,searchText: params.searchText}},classes: 'table table-responsive full-width',})$(document).on('keydown', function (event) {// 键盘翻页事件var e = event || window.event || arguments.callee.caller.arguments[0];if (e && e.keyCode == 38 || e && e.keyCode == 37) {//上,左// 上一页$('.page-pre').click()}if (e && e.keyCode == 40 || e && e.keyCode == 39) {//下,右// 下一页$('.page-next').click()}})})

后端接口

MeituController

package com.easy.kotlin.controllerimport com.easy.kotlin.chapter11_kotlin_springboot.dao.ImageRepository
import com.easy.kotlin.chapter11_kotlin_springboot.entity.Image
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.servlet.ModelAndView/*** Created by jack on 2017/7/22.*/@Controller
class MeituController {@Autowiredvar imageRepository: ImageRepository? = null@RequestMapping(value = "meituJson", method = arrayOf(RequestMethod.GET))@ResponseBodyfun wotuJson(@RequestParam(value = "page", defaultValue = "0") page: Int,@RequestParam(value = "size", defaultValue = "10") size: Int): Page<Image>? {return getPageResult(page, size)}@RequestMapping(value = "meituSearchJson", method = arrayOf(RequestMethod.GET))@ResponseBodyfun wotuSearchJson(@RequestParam(value = "page", defaultValue = "0") page: Int,@RequestParam(value = "size", defaultValue = "10") size: Int,@RequestParam(value = "searchText", defaultValue = "") searchText: String): Page<Image>? {return getPageResult(page, size, searchText)}@RequestMapping(value = "meituSearchFavoriteJson", method = arrayOf(RequestMethod.GET))@ResponseBodyfun wotuSearchFavoriteJson(@RequestParam(value = "page", defaultValue = "0") page: Int,@RequestParam(value = "size", defaultValue = "10") size: Int,@RequestParam(value = "searchText", defaultValue = "") searchText: String): Page<Image>? {return getFavoritePageResult(page, size, searchText)}@RequestMapping(value = "meituView", method = arrayOf(RequestMethod.GET))fun meituView(): ModelAndView {return ModelAndView("meituView")}@RequestMapping(value = "meituFavoriteView", method = arrayOf(RequestMethod.GET))fun meituFavoriteView(): ModelAndView {return ModelAndView("meituFavoriteView")}private fun getPageResult(page: Int, size: Int): Page<Image>? {val sort = Sort(Sort.Direction.DESC, "id")val pageable = PageRequest(page, size, sort)return imageRepository?.findAll(pageable)}private fun getPageResult(page: Int, size: Int, searchText: String): Page<Image>? {val sort = Sort(Sort.Direction.DESC, "id")val pageable = PageRequest(page, size, sort)if (searchText == "") {return imageRepository?.findAll(pageable)} else {return imageRepository?.search(searchText, pageable)}}private fun getFavoritePageResult(page: Int, size: Int, searchText: String): Page<Image>? {val sort = Sort(Sort.Direction.DESC, "id")val pageable = PageRequest(page, size, sort)if (searchText == "") {val allFavorite = imageRepository?.findAllFavorite(pageable)return allFavorite} else {val searchFavorite = imageRepository?.searchFavorite(searchText, pageable)return searchFavorite}}}

ImageRepository

package com.easy.kotlin.chapter11_kotlin_springboot.daoimport com.easy.kotlin.chapter11_kotlin_springboot.entity.Image
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.PagingAndSortingRepository
import org.springframework.data.repository.query.Param/** Created by jack on 2017/7/17.@Query注解里面的value和nativeQuery=true,意思是使用原生的sql查询语句.
sql模糊查询like语法,我们在写sql的时候是这样写的like '%?%'但是在@Query的value字符串中, 这样写like %?1%*/interface ImageRepository : PagingAndSortingRepository<Image, Long> {@Query("SELECT a from #{#entityName} a where a.isDeleted=0 and a.category like %?1%")fun findByCategory(category: String): MutableList<Image>@Query("select count(*) from #{#entityName} a where a.isDeleted=0 and a.url = ?1")fun countByUrl(url: String): Int@Query("SELECT a from #{#entityName} a where a.isDeleted=0 and a.category like %:searchText%")fun search(@Param("searchText") searchText: String, pageable: Pageable): Page<Image>@Query("SELECT a from #{#entityName} a where a.isDeleted=0 and a.isFavorite=1")fun findAllFavorite(pageable: Pageable): Page<Image>@Query("SELECT a from #{#entityName} a where a.isDeleted=0 and a.isFavorite=1 and a.category like %:searchText%")fun searchFavorite(@Param("searchText") searchText: String, pageable: Pageable): Page<Image>}

前台请求第一页的数据, 参数 &page=1&start=0&limit=50

后台得不到数据, 并提示 Page 1 of 1 containing UNKNOWN instances

原因page从0开始不是从1开始

解决方式把page修改为0

...pageNumber: 0,     //初始化加载第一页,默认第一页pageSize: 10,      //每页的记录行数(*)...

完整工程源代码

https://github.com/EasyKotlin/chatper15_net_io_img_crawler

参考连接:

Page 1 of 1 containing UNKNOWN instances when findByUserNameLike(String userName,Pageable pageable)

https://jira.spring.io/browse/DATAJPA-225

解决Spring Spring Data JPA 错误: Page 1 of 1 containing UNKNOWN instances相关推荐

  1. Spring Boot Data JPA

    Spring Data JPA简介 用来简化创建 JPA 数据访问层和跨存储的持久层功能. Spring Data JPA提供的接口 Repository:最顶层的接口,是一个空的接口,目的是为了统一 ...

  2. jpa Page 1 of 0 containing UNKNOWN instances错误

    作者:LoveEmperor-王子様 一.问题:Page 1 of 0 containing UNKNOWN instances 这个问题一般是你多个条件查询时,有条件添加进去了,但为空条件: 例: ...

  3. intellij选择困难症Spring Batch/Data JPA/Integration/MVC/Security/Web Flow/Web Services到底选哪个?

    新建工程碰到这么个东西... um...首先想说这些并不是非选不可的,只是根据你的需要,一些初始化的工程结构模板,让你减少点工作量而已. 选项 作用 Spring Batch   Data Data ...

  4. Page size must not be less than onePage 1 of 1 containing UNKNOWN instances

    出错的场景:前端easyui,在使用spring data jpa 分页时,出现的 最后最后是一个小问题,在属性驱动page,rows的拼写错误 --开始是摸不着头脑的查看了 在JPA分页实现类构造中 ...

  5. 继解决Spring data jpa 批量插入重写saveAll()后遇到符号不兼容问题

    问题描述 问题: 之前为解决Spring data jpa 批量插入/删除(saveAll()/deleteAll())速度慢的问题 重写了saveAll()方法,用自定义拼接sql的方法组装sql, ...

  6. 对Spring Data JPA中的page对象下的content属性里的实体类对象转换为dto对象

    对Spring Data JPA中的page对象下的content属性里的实体类对象转换为dto对象. 刚开始试遍历content,进行转换,添加到新的list中,再set进去page.后来发现pag ...

  7. 解决Spring data jpa 批量插入/删除(saveAll()/deleteAll())速度慢的问题

    问题描述: 项目中使用到了Spring data jpa技术,调用 JpaRepository.saveAll()/deleteAll()方法对list中的数据进行插入/删除时,发现速度特别慢,数据量 ...

  8. Spring Data Jpa 实体类自动创建数据库表失败解决

    先说一下我遇到的这个问题,首先我是通过maven创建了一个spring boot的工程,引入了Spring data jpa,结果实体类创建好之后,运行工程却没有在数据库中自动创建数据表. 找了半天发 ...

  9. 解决spring data jpa saveAll() 保存过慢

    spring data jpa saveAll() 保存过慢 问题发现 今天在生产环境执行保存数据时 影响队列中其他程序的运行 随后加日志排查 发现 执行 4500条 insert操作时 耗时 9分钟 ...

最新文章

  1. 陕西信息计算机学校,陕西计算机信息专业学校
  2. mysql客户端连接hive_连接Hive的客户端界面工具–SQuirrel SQL Client
  3. ICCV2017: Unlabeled Samples Generated by GAN Improve the Person Re-Identification Baseline in Vitro
  4. 引用用户控件图片无法
  5. 洛谷 P3383 【模板】线性筛素数
  6. 百度代码规范 -- PHP
  7. Python 协程 asyncio 极简入门与爬虫实战
  8. 【转】C#调用WebService实例和开发
  9. 阅读笔记:基础知识(Java篇)
  10. Python中使用libsvm
  11. 【MyBatis框架】mybatis逆向工程自动生成代码
  12. python的列表方法_Python列表的常用方法
  13. Android Studio用不了jar
  14. JavaScript之jQuery
  15. 哈夫曼编码C++实现
  16. 移动光猫超级管理员密码获取
  17. HDCP @ Locality Check
  18. 如何将一个HTML页面嵌套在另一个页面中
  19. 【吐槽】B站大量番剧下架,程序猿们这时都在干什么?
  20. Kettle基本使用(四) —— 应用的使用

热门文章

  1. buildroot编译和使用
  2. 比尔-盖茨出席新世代厕所博览会,展示新一代卫生产品
  3. 十年Hello World,写的是人生
  4. 力扣 23. 合并K个升序链表
  5. HashMap的put方法原理
  6. 2021-2025年中国健身训练软件行业市场供需与战略研究报告
  7. 主DNS、辅助DNS、缓存DNS和基于CDN的利用DNS服务器实现负载均衡
  8. 记一次tomcat、gateway配置SSL,使用https访问
  9. 开启电脑替我记忆之路
  10. 织梦插件教程新老版本通用织梦插件大全