当前位置: 代码迷 >> J2EE >> org.hibernate.exception.SQLGrammarException,该怎么处理
  详细解决方案

org.hibernate.exception.SQLGrammarException,该怎么处理

热度:179   发布时间:2016-04-17 23:08:48.0
org.hibernate.exception.SQLGrammarException
我配置了hibernate ,并生成实体类,但是在操作数据时,感觉生成的语句很奇怪都不像正常的语句的。
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="test">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_catalog">test</property>
        <property name="hibernate.default_schema">test</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
          <mapping resource="com/medical/model/Test.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Test.java:
package com.medical.model;

// Generated 2015-6-21 22:55:21 by Hibernate Tools 3.4.0.CR1

/**
 * Test generated by hbm2java
 */
public class Test implements java.io.Serializable {

private String test;

public Test() {
}

public Test(String test) {
this.test = test;
}

public String getTest() {
return this.test;
}

public void setTest(String test) {
this.test = test;
}

}


MyResource.java:
     Test t=new Test();
     Configuration config = new Configuration().configure();
     SessionFactory sessionFactory=config.buildSessionFactory();
     Session session=sessionFactory.openSession();
     t.setTest("abc");
     //TestHome th=new TestHome();
     //th.attachDirty(t);
     session.beginTransaction(); //启动事务
     //session.save(t);
     session.save(t);//.saveOrUpdate(t);
     session.getTransaction().commit(); //提交事务
        return "Got it!";





HTTP Status 500 - org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update


type Exception report

message org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update

description The server encountered an internal error that prevented it from fulfilling this request.

exception 
javax.servlet.ServletException: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:423)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



root cause 
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:262)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:178)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
com.medical.MyResource.getIt(MyResource.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:308)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:291)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1140)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:403)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



root cause 
java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.test (test) values ('abc')' at line 1
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1346)
com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1047)
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:262)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:178)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
com.medical.MyResource.getIt(MyResource.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:308)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:291)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1140)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:403)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



note The full stack trace of the root cause is available in the Apache Tomcat/7.0.62 logs.


Apache Tomcat/7.0.62


感觉生成的构思sql语句相当奇怪:'.test (test) values ('abc')'













------解决思路----------------------
cfg 文件中catalog ,schema 去掉,加入hibernate .format sql 设为true .针对此问题,没必要的属性不需要加入。确保jar 以及测试类没有问题。你在试试吧。
  相关解决方案