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

编辑: admin 分类: java 发布时间: 2021-12-03 来源:互联网
目录
  • JPA添加Pageable实现翻页时报错
    • 添加别名就可以解决
  • Spring JPA异常的处理
    • 解决方法

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文件中

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

【本文由:http://www.1234xp.com/cdn.html 提供,感谢支持】