当前位置: 首页 > 知识库问答 >
问题:

尝试在Hibernate中插入记录时获取SQLGrammarException

田嘉澍
2023-03-14

我收到以下异常-
org.hibernate.exception.sqlgrammarexception:无法插入:[Employee]
以下是详细信息:

hibernate.cfg.xml

    <hibernate-configuration>
    <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
            <property name="hibernate.connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property>
            <property name="hibernate.connection.url">jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:/Hibernate.accdb</property>
            <property name="connection.username"></property>
            <property name="connection.password"></property>
            <property name="connection.pool_size">1</property>
            <property name="current_session_context_class">thread</property>
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">update</property>
            <mapping resource="Employee.hbm.xml" />
    </session-factory>
    </hibernate-conf

employee.hbm.xml

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>
</hibernate-mapping>
public class Employee {
   private int id;
   private String firstName; 
   private String lastName;   
   private int salary;  

   public Employee() {}
   public Employee(String fname, String lname, int salary) {
      this.firstName = fname;
      this.lastName = lname;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public void setId( int id ) {
      this.id = id;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName( String first_name ) {
      this.firstName = first_name;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName( String last_name ) {
      this.lastName = last_name;
   }
   public int getSalary() {
      return salary;
   }
   public void setSalary( int salary ) {
      this.salary = salary;
   }
}
import java.util.List; 
import java.util.Date;
import java.util.Iterator; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManageEmployee {
   private static SessionFactory factory; 
   public static void main(String[] args) {
      try{
         factory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      }
      ManageEmployee ME = new ManageEmployee();

      /* Add few employee records in database */
      Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);
      Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
      Integer empID3 = ME.addEmployee("John", "Paul", 10000);
}
 public Integer addEmployee(String fname, String lname, int salary){
      Session session = factory.openSession();
      Transaction tx = null;
      Integer employeeID = null;
      try{
         tx = session.beginTransaction();
         Employee employee = new Employee(fname, lname, salary);
         employeeID = (Integer) session.save(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return employeeID;
   }

}

请帮助解决异常。提前道谢!

共有1个答案

诸葛亮
2023-03-14

Hibernate库中不存在MS Access方言。这会导致Hibernate无法检测标识列,也无法按照MS访问所需的方式操作。

org.hibernate.exception.sqlgrammarexception:无法插入:[Employee]hibernate:插入到Employee(first_name,last_name,salary)值(?,?,?)选择scope_identity()

select scope_identity()是MS Access不支持的TRANSACT-SQL实例

    null

有关如何使用hibernate与MS Access?

 类似资料:
  • 问题内容: 我正处在角度火种的教程阶段,并尝试了消息和子帖子,出于某些奇怪的原因,当我明确尝试显示子对象时,我看不到子对象,但是在控制台的父节点上展开时却可以看到子对象。这些消息在ng- repeat所在的html中正确显示,所以我知道至少得到了消息,尽管没有孩子。 我以为angularfire-seed utils可能会砍掉或切掉一些孩子,所以我回到了直火基地 这就是我所拥有的: 在此先感谢您指

  • 当我进行插入时,在traduction(idMot references mot)中的外键为null,在前面发送正确的外键。我最终出现了这个错误:not-null属性引用了一个空值或临时值:com.virtual.expertise.mydico.model.traduction.mot 我想我在我的模型中做错了什么,但仍然不知道是什么。 下面是交易表: 下面是我的WebService:(其中id

  • 问题内容: 我具有以下表格结构,为方便起见,我仅标记单个列 () ( 这是标识列 ,..) ( 这是标识列 ,,) Table_1和Table_2之间的关系是 现在,我创建了一个名为的表类型(仅包含为列,并且从我的C#应用​​程序中发送了多个记录)。我已经实现了所需的批量插入功能。 我需要的是,当我记录插入来自发言权下面的语句,我想捕捉的用于插入的每个记录 现在说如果插入了2条记录,我想捕获这2条

  • 我有两个实体类,如下所示- 我在保存子记录时出错。 错误- Hibernate:插入子(名称,child\u id)值(?,)2022-07-19 23:12:31.727警告20940---[nio-8080-exec-2]o.h.engine.jdbc.spi。SqlExceptionHelper:SQL错误:0,SQLState:23502 2022-07-19 23:12:31.727错误

  • 我在Aerospike数据库中有一个名称空间:test and set:user。我通过控制台上的以下命令在users中添加了四条记录: 通过aql命令,我可以查看这四条记录。aql 我知道一个接一个地获取记录的方法,它在我这边运行得很好,但这对于我的任务来说是非常昂贵的操作。我想读取多个记录(批读取)并对其执行多个算法。我从https://www.aerospike.com/docs/clien

  • 问题内容: 我使用以下查询将多个记录插入到表中: 由于查询要插入多个记录,因此无法使用 SCOPE_IDENTITY 来检索PK。有什么方法可以获取最近插入记录的ID? 问题答案: SCOPE_IDENTITY()将正确为您提供LAST ID。您需要将其与@@ Rowcount结合使用,以提供ID范围。 正如其他Richard指出的那样 ,这仅在您将增量设置为1时有效 例如: 执行此操作的另一种方