org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1, column 8

时间:2022-06-13 16:00:20

1、错误描述

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from t_student_info ]
	at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
	at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
	at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
	at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
	at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
	at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
	at com.you.ssh.dao.impl.StudentDaoImpl.findAll(StudentDaoImpl.java:74)
	at com.you.ssh.dao.impl.StudentDaoImpl$$FastClassBySpringCGLIB$$c4e653c7.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
	at com.you.ssh.dao.impl.StudentDaoImpl$$EnhancerBySpringCGLIB$$be35b18a.findAll(<generated>)
	at com.you.ssh.test.dao.StudentDaoTest.testFindAll(StudentDaoTest.java:53)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	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.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2、错误原因

     这里调用方法this.getCurrentSession().createQuery(sql.toString()).list(),是利用Hibernate HQL查询,而不是SQL查询;方法是HQL查询,但是SQL语句却是SQL查询方法语句,这样方法和参数不一致导致出错

/**
 *
 * 项目名称:SSH
 * 项目包名:com.you.ssh.dao.impl
 * 文件名称:StudentDaoImpl.java
 * 类型名称:StudentDaoImpl
 * 创建作者:游海东
 * 创建日期:2017-2-21
 * 创建时间:下午12:02:56
 * 版权所有:
 * 项目版本:V1.0
 */
package com.you.ssh.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.you.ssh.dao.StudentDao;
import com.you.ssh.model.Student;

/**
 * 学生信息
 * 文件名称:StudentDaoImpl.java
 * 文件类型:StudentDaoImpl
 * 文件包名:com.you.ssh.dao.impl
 * 创建作者:游海东
 * 创建日期:2017-2-21
 * 创建时间:下午12:02:56
 */
@Repository("studentDao")
@Transactional
public class StudentDaoImpl implements StudentDao 
{
	@Autowired
	private SessionFactory sessionFactory;
	
	/**
	 * 
	 * 方法描述:
	 * 创建作者:游海东
	 * 创建日期:2017-2-21
	 * 创建时间:下午2:09:50
	 * 方法名称:getCurrentSession
	 * 方法类型:StudentDaoImpl
	 * 返回类型:Session
	 * @return
	 */
	private Session getCurrentSession() 
	{
		return this.sessionFactory.getCurrentSession();
	}
	
	/**
	 * 功能描述:
	 * 方法名称:findAll
	 * 创建作者:游海东
	 * 创建日期:2017-2-21
	 * 创建时间:下午2:01:04
	 * @return
	 */
	@SuppressWarnings({ "unchecked"})
	@Override
	public List<Student> findAll()
	{
		List<Student> list = new ArrayList<Student>();
		StringBuilder sql = new StringBuilder();
		sql.append("select * from t_student_info ");
		list = this.getCurrentSession().createQuery(sql.toString()).list();
		
		return list;
	}

}

3、解决办法

(1)修改sql语句

(2)调用Hibernate SQL查询方法