【问题标题】:org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generatororg.hibernate.exception.SQLGrammarException:无法获取增量生成器的初始值
【发布时间】:2014-07-01 23:47:27
【问题描述】:

您好,我正在尝试使用 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;
    }
 }

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

提前致谢

【问题讨论】:

  • 你的表结构是什么?

标签: java hibernate jsp


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2018-10-04
      • 2014-05-13
      • 1970-01-01
      • 2014-08-03
      • 2019-02-08
      相关资源
      最近更新 更多