SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped相关的知识,希望对你有一定的参考价值。

非常诡异的报错,信息如下:
org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(*) from User u where u.userName=? and u.userPassword=? ]

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

实现类是这么写的:

@Component("userDAOImpl")
public class UserDAOImpl implements UserDAO
private final String LOGIN_HQL = "select count(*) from User u where u.userName=? and u.userPassword=? ";

private HibernateTemplate hibernateTemplate;

@Resource(name = "hibernateTemplate")
public void setHibernateTemplate(HibernateTemplate hibernateTemplate)
this.hibernateTemplate = hibernateTemplate;


@Override
public String login(User user)
String flag = null;
Session session = hibernateTemplate.getSessionFactory().openSession();
long result = (Long) session.createQuery(LOGIN_HQL)
.setString(0, user.getUserName())
.setString(1, user.getUserPassword())
.uniqueResult();
if (result > 0)
flag = "success";
else
flag = "fail";
return flag;



配置文件和Model类没有问题,数据库也是用正向工程自动生成的,但是运行的时候说匹配不上User,这是为什么????
诚心求教,望大神指点

错误一:org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [spring.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

解决办法:aopalliance.jar

错误二:java.lang.NoClassDefFoundError: org/springframework/expression/PropertyAccessor

解决办法:加上spring-expression-3.2.9.RELEASE.jar

错误三:nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around

解决办法:加入aspectjweaver.jar

错误四:nested exception is java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager

解决办法:加入hibernate-commons-annotations-4.0.4.Final.jar

错误五:nested exception is java.lang.NoClassDefFoundError: org/jboss/logging/BasicLogger

解决办法:使用Hibernate4.0后,需要引入jboss-logging.GA.jar

错误六:nested exception is java.lang.NoClassDefFoundError: javax/transaction/SystemException

解决办法:加入jboss-transaction-api_1.2_spec-1.0.0.Final.jar

错误七:nested exception is java.lang.NoClassDefFoundError: javax/persistence/Entity

解决办法:加入ejb3-persistence.jar

错误八:nested exception is java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery

解决办法:加入hibernate-jpa-2.1-api-1.0.0.Final.jar

参考技术A 你的配置文件没有 被加载起来

所以无法使用对象查询

只能使用SQL 查询追问

配置文件应该是被加载了的,因为我的数据库是用正向工程自动生成的, 如果配置文件没有被加载的话那么数据库不是应该生成不了吗?

追答

也有可能啊

出现这中错误情况有两种

1,配置文件没有加载到hibernate的实体列表里面

2,映射文件的字段与数据库字段不一致,或者名称不一致导致

就只有这两种情况

本回答被提问者和网友采纳
参考技术B 提示一下

1,xml<mapping resource="xxxxx"/>
2, annotation<mapping class"org/hibernate/tutorial/domain/Event.hbm.xml"/>追问

我采用的是注解的方式,在Spring配置文件中写的是

参考技术C 对,楼下说的对,检查一下配置文件!提示很清楚了!

org.hibernate.hql.QueryExecutionRequestException:org.hibernate.hql.QueryExecutionRequestException:



springData学习资料 【http://blog.csdn.net/lw_power/article/details/51296353】

【JPA报错】
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is org.springframework.dao.InvalidDataAccessApiUsageException:
org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations
[delete com.jspxcms.core.domain.CmsProjectMember bean where bean.cmsProjectNew.id in (?1)];
nested exception is java.lang.IllegalStateException: org.hibernate.hql.QueryExecutionRequestException:
Not supported for DML operations [delete com.jspxcms.core.domain.CmsProjectMember bean where bean.cmsProjectNew.id in (?1)] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)

【原因】
类的dao删除方法是@Modifying

@Query("delete CmsProjectMember bean where bean.cmsProjectNew.id in (?1)")
public void deleteByProjectId(Collection<Integer> ids);

修改为:

@Modifying
@Query("delete CmsProjectMember bean where bean.cmsProjectNew.id in (?1)")
public void deleteByProjectId(Collection<Integer> ids);

===============网上资料===============

【http://blog.csdn.net/lw_power/article/details/51296353】

SpringData 学习(5)—— 使用 @Modifying 注解完成修改操作

以上我们做的都是查询,那要如何实现 修改、删除和添加呢? 
可以通过以下两种方式: 
(1)通过实现 CrudRepository 接口来完成(以后介绍); 
(2)通过 @Modifying 注解完成修改操作(注意:不支持新增)

我们依然使用 @Query 注解,但是还要附加注解 @Modifying

@Modifying
@Query("update Person set email = :email where lastName =:lastName")
void updatePersonEmailByLastName(@Param("lastName")String lastName,@Param("email")String email);
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

测试方法很简单了,暂时并不粘贴出来,因为后面我们要改的。 
控制台报错:

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:410)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:216)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy29.updatePersonEmailByLastName(Unknown Source)
    at com.liwei.springdata.service.PersonService.updateEmailByLastName(PersonService.java:15)
    at com.liwei.springdata.test.DataSourceTest.testUsingModifingAnnotation(DataSourceTest.java:154)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:362)
    at com.sun.proxy.$Proxy31.executeUpdate(Unknown Source)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:174)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:61)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:95)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:85)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:323)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    ... 32 more

关键看这行 Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query,说明我们缺少事务配置。 
于是 
1、在 spring 核心配置文件里引入事务管理器(这一步我们之前已经做了)

<context:component-scan base-package="com.liwei.springdata.service"></context:component-scan>

2、写好 Service 类的相关方法,在事务方法前面加上 @Transactional 注解。

@Service

public class PersonService {

@Autowired private PersonRepository personRepository;

@Transactional public void updateEmailByLastName(String lastName,String email){

personRepository.updatePersonEmailByLastName(lastName, email); }

}

测试代码

// 以上都是查询,现在我们要测试修改 @Test public void testUsingModifingAnnotation(){ personService.updateEmailByLastName("liwei", "[email protected]"); }

 

 

控制台打印:

技术分享

经过测试,通过上面的方式实现删除也是可以得,代码我就不贴出来了,大家可以具体尝试一下,很简单。 
技术分享

总结: 
(1)可以通过自定义的 JPQL 完成 UPDATE 和 DELETE 操作。 注意: JPQL 不支持使用 INSERT; 
(2)在 @Query 注解中编写 JPQL 语句, 但必须使用 @Modifying 进行修饰. 以通知 SpringData, 这是一个 UPDATE 或 DELETE 操作 
(3)UPDATE 或 DELETE 操作需要使用事务,此时需要定义 Service 层,在 Service 层的方法上添加事务操作; 
(4)默认情况下, SpringData 的每个方法上有事务, 但都是一个只读事务。 他们不能完成修改操作。




























以上是关于SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped的主要内容,如果未能解决你的问题,请参考以下文章

org.hibernate.hql.QueryExecutionRequestException:org.hibernate.hql.QueryExecutionRequestException:

org.hibernate.hql.ast.QuerySyntaxException

原因:org.hibernate.QueryException:节点没有数据类型:org.hibernate.hql.internal.ast.tree.MethodNode

hibernate hsql得到错误节点没有数据类型:org.hibernate.hql.ast.tree.IdentNode

休眠错误-org.hibernate.hql.internal.ast.QuerySyntaxException

org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:FETCH