Hibernate 无法执行JDBC批处理更新

Hibernate 无法执行JDBC批处理更新,hibernate,hibernate-mapping,Hibernate,Hibernate Mapping,我对hibernate框架比较熟悉,我试着运行我的第一个hibernate应用程序我遇到了以下异常 例外情况 Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) at org.

我对hibernate框架比较熟悉,我试着运行我的第一个hibernate应用程序我遇到了以下异常

例外情况

Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.java4s.insert.InsertMain.main(InsertMain.java:26) Caused by: 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 'insert (user, pass, sno) values ('krish', 'password', 1)' at line 1 at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2045)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1468)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)... 8 more Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert (user, pass, sno) values ('krish', 'password', 1)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144)
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>
        <property name="connection.driver-class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="show_sql">true</property>
        <property name="dilect">org.hibernate.dialect.MySQL.Dialect</property>
        <property name="hbm2dll.auto">update</property>
        <mapping resource="Insert.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
InsertMain(配置类)


insert
是保留的SQL关键字。不要将其用作表名。也不要使用
user
作为列的名称。

insert
是保留的SQL关键字。不要将其用作表名。也不要使用
user
作为列的名称。

现在JDBC批量更新异常已清除,但我得到了test.product找不到异常如果我手动创建表意味着,我的代码工作正常,代码中的错误是什么,为什么不自动创建表。因为hibernate属性是
hbm2ddl.auto
而不是
hbm2dll.auto
。DLL=动态链接库。DDL=数据定义语言。经过一番思考,我终于接受了你的建议。谢谢:)现在JDBC批量更新异常清除了,但我得到了测试。如果我手动创建表,产品找不到异常意味着,我的代码工作正常,代码中的错误是什么,为什么不自动创建表。因为hibernate属性是
hbm2ddl.auto
而不是
hbm2dll.auto
。DLL=动态链接库。DDL=数据定义语言。经过一番思考,我终于接受了你的建议。谢谢:)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.java4s.insert.Insert" table="insert">
        <id name="sno" column="sno"/>
        <property name="username" column="user"/>
        <property name="password" column="pass"/>
    </class>
</hibernate-mapping>
public class Insert {
    private int sno;
    private String username;
    private String password;    
    //setter and getter method;
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class InsertMain {

    public static void main(String[] args) {

        Configuration cfg=new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory factory=cfg.buildSessionFactory();
        Session session=factory.openSession();
        Transaction tx=session.beginTransaction();
        Insert i=new Insert();
        i.setSno(1);
        i.setUsername("krish");
        i.setPassword("password");
        session.save(i);
        System.out.println("Object saved successfully.....!!");
        tx.commit();
        session.close();
        factory.close();
    }
}