簡體   English   中英

Spring Hibernate:org.hibernate.exception.SQLGrammarException

[英]Spring hibernate: org.hibernate.exception.SQLGrammarException

我的DAO層代碼如下:

package com.app.dao;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.app.pojo.InterviewerPojo;

@Repository("interviewer_details_dao")
public class InterviewerDetailsDaoImpl implements InterviewerDetailsDao {

    @Resource(name="sessionFactory")
    private SessionFactory factory;
    /*private HibernateTemplate template;
    public void setSessionFactory(){
        template = new HibernateTemplate(factory);
    }*/
    /*private HibernateTemplate template = new HibernateTemplate(sessionFactory);*/
    @Override
    public void saveInterviewDetails(InterviewerPojo interviewerPojo) {

/*      Session sess = factory.getCurrentSession();
        try{
            sess.getTransaction().wasCommitted();
        Transaction trans = sess.beginTransaction();
        System.out.println("a");
        sess.saveOrUpdate(interviewerPojo);
        sess.getTransaction().commit();}
        catch(Exception e){
            throw e;
        }*/
        System.out.println("b");
        factory.getCurrentSession().save(interviewerPojo);
    /*  System.out.println(interviewerPojo);
        template.saveOrUpdate(interviewerPojo);
        System.out.println("b");*/

        System.out.println("done");

    }

}

如果我要從數據庫中獲取值,則運行與上述相同的代碼,如下所示: sessionFactory.getCurrentSession()。createQuery(“從emp m中選擇m).list; 但是,當我編寫另一頁來插入值時,則出現了問題 * 如您所見,我使用了相同的概念,我通過上面的查詢 * 從數據庫獲取的另一個MODEL(pojo)填充了MODEL(POJO)類 ,現在調試升至“ template.saveOrUpdate”並拋出異常,如下所示

org.hibernate.exception.SQLGrammarException:您的SQL語法有錯誤。 檢查與您的MySQL服務器版本對應的手冊以獲取正確的語法,以在org.hibernate.engine的org.hibernate.exception.internal.SQLStateConverter.convert(SQLStateConverter.java:100)的第1行附近使用“ hibernate_sequence的值” org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)上的org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler上的.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)處的.continueInvocation(AbstractStatementProxyHandler.java:129)在org.hibernate.id.SequenceGenerator處的$ Proxy24.executeQuery(Unknown Source)處。 org.hibernate.id.SequenceHiLoGenerator $ 1.getNextValue(SequenceHiLoGenerator.java:85)的org.hibernate.id.enhanced.OptimizerFactory $ LegacyHiLoAlgorithmOptimizer.generate(OptimizerFactory.java:350)的generateHolder(SequenceGenerator.java:112) hibernate.id.SequenceHiLoGenerat org.org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)或org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)處的generate(SequenceHiLoGenerator.java:82) .hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)位於org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)位於org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate( .java:49),位於org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90),位於org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:642),位於org.hibernate.internal.SessionImpl。在com.app.dao.Interview上的org.hibernate.internal.SessionImpl.save(SessionImpl.java:631)處保存(SessionImpl.java:635) com.app.service.SaveInterviewerDetailsS​​erviceImpl.saveDetails(SaveInterviewerDetailsS​​erviceImpl.java:25)處的erDetailsDaoImpl.saveInterviewDetails(InterviewerDetailsDaoImpl.java:40)在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)處在sun.reflect.NativeMethodAccessorImpl.Invoke0(Native Method) )處org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)處java.lang.reflect.Method.invoke(未知源)處sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)上的.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java :110)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)at org.springframework.aop.framework .JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)位於$ Proxy19.saveDetails(未知源)位於com.app.controllers.EmployeePromoteController.validateDetails(EmployeePromoteController.java:67)位於sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法) org.springframework.web.method.support.InvocableHandlerMethod處java.lang.reflect.Method.invoke處sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)處的sun.reflect.NativeMethodAccessorImpl.invoke(未知源)。 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod。 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)的org.springframework.web.servlet.mvc.method.annotat的java:96) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.org)上的ion.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)。 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)的org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)的org.springframework.web.servlet的java:923) org.apache中的javax.servlet.http.HttpServlet.service(HttpServlet.java:647)處的.FrameworkServlet.doPost(FrameworkServlet.java:789)在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)處位於org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)的catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java: 222),網址為org.apache.catalin org.apache.catalina.authalicator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)的org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:的a.core.StandardContextValve.invoke(StandardContextValve.java:123) 171)在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)在org.apache.catalina.valves.AccessLogValve.invoke(AccessReportValve.java:931)在org.apache.catalina.core.StandardEngineValve org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)的org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)的.invoke(StandardEngineValve.java:118) org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312)處的apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589)在java.util.concurrent.ThreadPoolExecutor.runWorker( java.util.concurrent.ThreadPoolExecutor $ Worker.run(U上的未知源)(U.java.lang.Thread.run(U上的未知源) 原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL語法有錯誤; 檢查與您的MySQL服務器版本相對應的手冊以獲取正確的語法,以在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native方法)的sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native方法)的第1行附近使用“ hibernate_sequence的值” sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知源)位於com.mysql.jdbc.Util.handleNewInstance(Util.java:407)處的com.mysql.jdbc.Util.handleNewInstance(Util.java:407)處的java.lang.reflect.Constructor.newInstance(未知源) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)處com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)處的.getInstance(Util.java:382)。 com.mysql.jdbc上的MysqlIO.checkErrorPacket(MysqlIO.java:3525)com.mysql.jdbc上的MysqlIO.sendCommand(MysqlIO.java:1986)com.mysql.jdbc上的com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140) com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)的com.mysql.jdbc的.ConnectionImpl.execSQL(ConnectionImpl.java:2626) 位於org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)的PreparedStatement.executeQuery(PreparedStatement.java:2273)位於org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement處)。 .reflect.NativeMethodAccessorImpl.invoke0(本機方法)(位於sun.reflect.NativeMethodAccessorImpl.invoke(未知源),位於java.lang.reflect.Method.invoke(未知源)處的sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源) .hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)... 61更多


POJO源代碼:

package com.app.pojo; 
import java.io.Serializable; 
import javax.persistence.*; 

@SuppressWarnings("serial") 
@Entity @Table(name="interviewer") 
public class InterviewerPojo implements Serializable {
    private int eid;

    private int vid; 

    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE) 
    private int i_eid; 

    private String i_name; 

    private String password; 

    public int getEid() { 
        return eid; 
    } 

    public void setEid(int eid) { 
        this.eid = eid; 
    } 
}

休眠配置:

<property name="hibernateProperties"> 
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
        <prop key="hibernate.formatsql">true</prop>
        <prop key="hibernate.showsql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">validate</prop>
    </props> 
</property>   

在錯誤日志中,它表明您正在使用MySQL。 但是,您配置的Hibernate方言是HSQLDialect。

org.hibernate.dialect.HSQLDialect

您應該刪除該方言屬性,Hibernate可以通過JDBC驅動程序自動檢測正確的方言。 或者您自己修改為正確的方言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM