web-dev-qa-db-fra.com

ERREUR: ORA-02289: la séquence n'existe pas - org.hibernate.exception.SQLGrammarException: impossible d'extraire ResultSet

Je crée une application de mise en veille prolongée de base pour la table des employés dans laquelle j'ajoute, met à jour, supprime et affiche l'enregistrement à l'aide de la classe ManageEmployee.

En fait, ManageEmployee est ma classe de test et j'utilise le fichier Employee.Java et Employee.hbm.xml pour faire les choses ci-dessus.

Détails: Base de données: Oracle 11 g Hibernate version: hibernate-release-4.3.8.Final Java Version: Java 1.8

Détails de la table des employés:

USER_ID NUMBER (15,0) No 1 1
FIRST_NAME VARCHAR2 (20 BYTE) Oui 2
LAST_NAME VARCHAR2 (20 BYTE) Oui 3
NUMÉRO DE SALAIRE (10,0) Oui 4

Journal des erreurs: vous pouvez voir dans le journal que l'erreur arrive 3 fois car j'ajoute l'objet employé 3 fois dans la classe ManageEmployee.Java avec la méthode addEmployee (...), donc je suppose que le problème vient du générateur de séquence pour la colonne id.

Ici, j'utilise un générateur naïf. i.g. Entier 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

Code du fichier 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(); 
      }
   }
}

Fichier de mappage:

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

Il manque un générateur de séquence à la classe des employés, donc hibernate ne peut pas insérer la ligne. cela devrait être quelque chose comme ça qui augmente la colonne de nombre d'id de 1:

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

après avoir lu séquences Oracle et comment elles fonctionnent, ainsi que indiquant à Hibernate que vous utilisez une SEQUENCE (section 5.1.2.2.4) plutôt qu'un générateur natif, J'ai corrigé mes erreurs.

J'ai créé une séquence dans la base de données Oracle par la requête ci-dessous et mis à jour mon fichier 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