java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > JPA添加Pageable翻页报错

JPA添加Pageable实现翻页时报错的问题

作者:alex_fung

这篇文章主要介绍了解决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文件中

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文