我是靠谱客的博主 酷炫饼干,最近开发中收集的这篇文章主要介绍JPA添加Pageable实现翻页时报错的问题,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

JPA添加Pageable实现翻页时报错

@Query("from Subject where name like CONCAT('%',:name,'%') and isDelete = 0 order by createTime desc")
Page<Subject> findSubjectByName(@Param("name") String name, Pageable page);

报错:

select count(where) from com.swj.entity.Subject where name like CONCAT('%',:name,'%') and isDelete = 0

添加别名就可以解决

@Query("select t from Subject t where t.name like CONCAT('%',:name,'%') and t.isDelete = 0 order by createTime desc")
Page<Subject> findSubjectByName(@Param("name") String name, Pageable page);

Spring JPA异常的处理

Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Unknown entity: com.ecs.model.User; nested exception is java.lang.IllegalArgumentException: Unknown entity: com.ecs.model.User
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:418)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy14.save(Unknown Source)
at ecs.Test1.main(Test1.java:43)
Caused by: java.lang.IllegalArgumentException: Unknown entity: com.ecs.model.User
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:878)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
at com.sun.proxy.$Proxy13.persist(Unknown Source)
at com.ecs.dao.UserDaoImpl.save(UserDaoImpl.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
... 6 more

解决方法

把persistence.xml放在src下的META-INF文件中

以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是酷炫饼干为你收集整理的JPA添加Pageable实现翻页时报错的问题的全部内容,希望文章能够帮你解决JPA添加Pageable实现翻页时报错的问题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(147)

评论列表共有 0 条评论

立即
投稿
返回
顶部