org.hibernate.exception.SQLGrammarException:无法获取增量生成器的初始值

时间:2014-05-14 04:56:48

标签: java hibernate jsp

您好我正在尝试使用hibernate创建简单的jsp注册表单,其中我将获得以下异常

type Exception report

messageInternal Server Error

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

exception

org.apache.jasper.JasperException: org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator

root cause

org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator

root cause

com.mysql.jdbc.exceptions.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 'index) from user_registration' at line 1

这是我的

Userregistration.hbm.xml

<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 9, 2011 6:53:58 PM by Hibernate Tools 3.2.1.GA -->

          <hibernate-mapping>
          <class name="com.hibernateClass.UserRegistration" table="user_registration">
          <id name="index">
          <generator class="increment"></generator>
          </id>
          <property name="userName"></property>
          <property name="password"></property>
          <property name="email"></property>
          </class>
          </hibernate-mapping>

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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

      <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/employee</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="show_sql">true</property>
      <mapping resource="com/hibernateClass/UserRegistration.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

UserRegistration.java

public class UserRegistration  implements java.io.Serializable {


      int index;
      String userName;
      String password;
 String email;
    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
 }

我该如何处理这个例外 并得到我想要的输出

提前致谢

2 个答案:

答案 0 :(得分:0)

也许'index'是一个关键字。在sql编程中要小心,有些单词看起来像关键字,你应该避免它们

答案 1 :(得分:-1)

是的,我们不能在代码中使用SQL的关键字。否则会显示此类错误。

注意:您可以通过在SQL编辑器(例如SQLYOG)中写入大写字母来检查SQL关键字。这是屏幕截图。enter image description here