1

I want to execute an update query in my oracle database. But I'm getting the following error:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/C:/hibernate/lib/required/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding()
WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Hibernate: update ëmp1 set ename=?, esal=?, eaddr=? where eno=?
Employee updation failure
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at com.durgasoft.test.Test.main(Test.java:33)
Caused by: java.sql.BatchUpdateException: ORA-00942: table or view does not exist

    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10296)
    at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:216)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 8 more

Files in the project:

Pojo class code Employee.java

package com.MyApp.pojo;

public class Employee {
    private int eno;
    private String ename;
    private float esal;
    private String eaddr;
    
    
    // getter and setters
}

Main method Test.java

package com.durgasoft.test;

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

import com.MyApp.pojo.Employee;

public class Test {

    public static void main(String[] args) {
        
        Configuration config=null;
        SessionFactory sessionFactory=null;
        Session session=null;
        Transaction tx=null;
        
        try 
        {
            config = new Configuration();
            config.configure();
            sessionFactory=config.buildSessionFactory();
            session=sessionFactory.openSession();
            tx=session.getTransaction();
            tx.begin();
            Employee emp=new Employee();
            emp.setEno(111);
            emp.setEname("XXX");
            emp.setEsal(1000);
            emp.setEaddr("ABCD");
            session.update(emp);
            tx.commit();
            System.out.println("Employee Updated Successfully");
            
            
        }
        catch(Exception e)
        {
            tx.rollback();
            System.out.println("Employee updation failure");
            e.printStackTrace();
            
        }
        finally 
        {
            
            session.close();
            sessionFactory.close();
        }

    }

}

Hibernate configuration file hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="org.hibernate.driver_Class">oracle.jdbc.OracleDriver</property>
            <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
            <property name="hibernate.connection.username">username</property>
            <property name="hibernate.connection.password">password</property>
            <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
            <property name="hibernate.show_sql">true</property>
            <mapping resource="Employee.hbm.xml"/>
            
        
        </session-factory>
    
    </hibernate-configuration>

Hibernate Mapping file Employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    <class name="com.durgasoft.pojo.Employee" table="ëmp1">
    <id name="eno"/>
    <property name="ename"/>
    <property name="esal"/>
    <property name="eaddr"/>
    
    </class>
    
    </hibernate-mapping>

Dependencies:

Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
neeraj
  • 31
  • 1

1 Answers1

0

Not sure if you have created your table in your DB. Try these two options:

  1. Add table emp1 in DB and re run your application.
  2. Add below configuration in your hibernate config file.
<property name="hbm2ddl.auto">update</property>

You can refer this link for various values you can use.

https://stackoverflow.com/a/23678638/8148637

Edit:

You are using session.update(emp) to update a table that is empty.Instead, Use:

session.saveOrUpdate(emp)

KnockingHeads
  • 1,569
  • 1
  • 22
  • 42
  • ``` SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/C:/hibernate/lib/required/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding() WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler ``` – neeraj May 01 '21 at 13:20
  • WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release Hibernate: update emp set ename=?, esal=?, eaddr=? where eno=? Employee Updated Successfully. but table is empty. – neeraj May 01 '21 at 13:21
  • What did you change that it is now updating the table. Did you change the table name? – KnockingHeads May 01 '21 at 13:28
  • yes after changing table name, it start displaying this message. – neeraj May 01 '21 at 13:32
  • You have no record in your table. You use session.save to insert a new record and session.update to update that record. Here, your table is already empty and you are using update. Try using session.saveOrUpdate(emp) in your case. It will insert employee if its not there. If it is present, then it updates it. – KnockingHeads May 03 '21 at 07:22