web-dev-qa-db-ja.com

エラー:ORA-02289:シーケンスが存在しません-org.hibernate.exception.SQLGrammarException:ResultSetを抽出できませんでした

ManageEmployeeクラスの助けを借りて、レコードを追加、更新、削除、表示する従業員テーブル用の基本的な休止状態アプリケーションを作成しています。

実際、ManageEmployeeは私のテストクラスであり、上記のことを行うためにEmployee.JavaおよびEmployee.hbm.xmlファイルを使用しています。

詳細:データベース:Oracle 11 g Hibernateバージョン:hibernate-release-4.3.8.Final Java Version:Java 1.8

従業員テーブルの詳細:

USER_ID NUMBER(15,0)いいえ1 1
FIRST_NAME VARCHAR2(20 BYTE)はい2
LAST_NAME VARCHAR2(20 BYTE)はい3
給与番号(10,0)はい4

エラーログ:EmployeeオブジェクトをaddEmployee(...)メソッドでManageEmployee.Javaクラスに3回追加しているため、エラーが3回発生していることがログでわかります。 id。

ここでは、単純なジェネレーターを使用しています。例整数empID1 = ME.addEmployee( "Zara"、 "ALi"、1000);

May 12, 2015 3:34:39 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-02289: sequence does not exist

org.hibernate.exception.SQLGrammarException: could not extract ResultSet
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.Java:80)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.Java:49)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Java:126)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Java:112)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.Java:91)
        at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.Java:122)
        at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.Java:115)
        at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.Java:117)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.Java:209)
        at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.Java:55)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.Java:194)
        at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.Java:49)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.Java:90)
        at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.Java:715)
        at org.hibernate.internal.SessionImpl.save(SessionImpl.Java:707)
        at org.hibernate.internal.SessionImpl.save(SessionImpl.Java:702)
        at ManageEmployee.addEmployee(ManageEmployee.Java:47)
        at ManageEmployee.main(ManageEmployee.Java:23)
Caused by: Java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

        at Oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.Java:450)
        at Oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.Java:399)
        at Oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.Java:1059)
        at Oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.Java:522)
        at Oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.Java:257)
        at Oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.Java:587)
        at Oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.Java:225)
        at Oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.Java:53)
        at Oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.Java:774)
        at Oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.Java:925)
        at Oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.Java:1111)
        at Oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.Java:4798)
        at Oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.Java:4845)
        at Oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.Java:1501)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.Java:82)
        ... 13 more
May 12, 2015 3:34:39 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 2289, SQLState: 42000
May 12, 2015 3:34:39 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-02289: sequence does not exist

org.hibernate.exception.SQLGrammarException: could not extract ResultSet
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.Java:80)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.Java:49)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Java:126)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Java:112)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.Java:91)
        at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.Java:122)
        at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.Java:115)
        at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.Java:117)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.Java:209)
        at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.Java:55)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.Java:194)
        at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.Java:49)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.Java:90)
        at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.Java:715)
        at org.hibernate.internal.SessionImpl.save(SessionImpl.Java:707)
        at org.hibernate.internal.SessionImpl.save(SessionImpl.Java:702)
        at ManageEmployee.addEmployee(ManageEmployee.Java:47)
        at ManageEmployee.main(ManageEmployee.Java:24)
Caused by: Java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

        at Oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.Java:450)
        at Oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.Java:399)
        at Oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.Java:1059)
        at Oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.Java:522)
        at Oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.Java:257)
        at Oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.Java:587)
        at Oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.Java:225)
        at Oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.Java:53)
        at Oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.Java:774)
        at Oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.Java:925)
        at Oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.Java:1111)
        at Oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.Java:4798)
        at Oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.Java:4845)
        at Oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.Java:1501)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.Java:82)
        ... 13 more
May 12, 2015 3:34:39 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 2289, SQLState: 42000
May 12, 2015 3:34:39 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-02289: sequence does not exist

org.hibernate.exception.SQLGrammarException: could not extract ResultSet
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.Java:80)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.Java:49)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Java:126)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Java:112)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.Java:91)
        at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.Java:122)
        at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.Java:115)
        at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.Java:117)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.Java:209)
        at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.Java:55)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.Java:194)
        at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.Java:49)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.Java:90)
        at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.Java:715)
        at org.hibernate.internal.SessionImpl.save(SessionImpl.Java:707)
        at org.hibernate.internal.SessionImpl.save(SessionImpl.Java:702)
        at ManageEmployee.addEmployee(ManageEmployee.Java:47)
        at ManageEmployee.main(ManageEmployee.Java:25)
Caused by: Java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

        at Oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.Java:450)
        at Oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.Java:399)
        at Oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.Java:1059)
        at Oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.Java:522)
        at Oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.Java:257)
        at Oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.Java:587)
        at Oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.Java:225)
        at Oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.Java:53)
        at Oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.Java:774)
        at Oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.Java:925)
        at Oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.Java:1111)
        at Oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.Java:4798)
        at Oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.Java:4845)
        at Oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.Java:1501)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.Java:82)
        ... 13 more
First Name: a  Last Name: b  Salary: 2000

ManageEmployee.Javaファイルコード:

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);

      /* List down all the employees */
      ME.listEmployees();

      /* Update employee's records */
     // ME.updateEmployee(empID1, 5000);

      /* Delete an employee from the database */
     // ME.deleteEmployee(empID2);

      /* List down new list of the employees */
     // ME.listEmployees();
   }
   /* Method to CREATE an employee in the database */
   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;
   }
   /* Method to  READ all the employees */
   public void listEmployees( ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         List employees = session.createQuery("FROM Employee").list(); 
         for (Iterator iterator = 
                           employees.iterator(); iterator.hasNext();){
            Employee employee = (Employee) iterator.next(); 
            System.out.print("First Name: " + employee.getFirstName()); 
            System.out.print("  Last Name: " + employee.getLastName()); 
            System.out.println("  Salary: " + employee.getSalary()); 
         }
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
   /* Method to UPDATE salary for an employee */
   public void updateEmployee(Integer EmployeeID, int salary ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Employee employee = 
                    (Employee)session.get(Employee.class, EmployeeID); 
         employee.setSalary( salary );
                 session.update(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
   /* Method to DELETE an employee from the records */
   public void deleteEmployee(Integer EmployeeID){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Employee employee = 
                   (Employee)session.get(Employee.class, EmployeeID); 
         session.delete(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
}

マッピングファイル:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <id name="user_id" type="int" column="user_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 user_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 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;
   }
public int getUser_id() {
        return user_id;
}
public void setUser_id(int user_id) {
        this.user_id = user_id;
}
}
8
user2682305

従業員クラスにはシーケンスジェネレータがないため、休止状態では行を挿入できません。 id countカラムを1増やすこのようなものでなければなりません:

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQUENCE1")
@SequenceGenerator(name="SEQUENCE1", sequenceName="SEQUENCE1", allocationSize=1)
private int user_id;
8
Asura

Oracleシーケンス とその機能、および SEQUENCEを使用していることをHibernateに伝える (セクション5.1.2.2.4)ではなくネイティブジェネレーターを読んだ後、エラーを修正しました。

以下のクエリでOracleデータベースにシーケンスを作成し、hbm.xmlファイルを更新しました

CREATE SEQUENCE TAB_CUSTOMER_SEQ
START WITH 1
MAXVALUE 999999999999999999999999999
MINVALUE 1
NOCYCLE
CACHE 20
NOORDER;
<id name="user_id" type="int" column="user_id">
          <generator class="sequence">
            <param name="sequence">TAB_CUSTOMER_SEQ</param>
          </generator>         
      </id>
4
user2682305