jeudi 21 mai 2015

org.hibernate.exception.SQLGrammarException: could not extract ResultSet

I have implemented a simple application in Hibernate-4. This application retrieve the value from a table. But when I try to get the record with this

(Booking) session.get(Booking.class, 3740456);

it gives me exception

INFO: HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet

So, to verify into the database regarding table and column name, I copy the query from the log statement and execute it. It gives me proper output.

I checked several questions & answers related to this question, but could able to derive the solution.

Hibernate.cfg.xml

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://ift.tt/UNuKEc">

<hibernate-configuration>
   <session-factory>

       <property name="connection.url">jdbc:oracle:thin:@SHKG9072DB:5030:TMSD10G2</property>
       <property name="connection.username">ICTDEV$EDI_APP</property>
       <property name="connection.password">p2II9JLIaea06</property>
       <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
       <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

       <property name="show_sql">true</property>

       <property name="format_sql">true</property>

       <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
       <property name = "hibernate.jdbc.lob.non_contextual_creation">true</property>  


       <property name="connection.pool_size">1</property>
       <property name="current_session_context_class">thread</property>

       <mapping class="com.hibernate.demo.Booking"/> 

    </session-factory>
</hibernate-configuration>

Booking.java

 package com.hibernate.demo;

 import java.io.Serializable;

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.Table;

 @Entity
 @Table(name="BOOKING")
 public class Booking implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id 
private int id;

@Column(name="SITE")
private String site;


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = (id);
}

public String getSite() {
    return site;
}

public void setSite(String site) {
    this.site = (site);
}

}

Main.java

package com.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class Main {

public static void main(String[] args) {
    System.out.println("Hello World");
    System.out.println("Trying to create a test connection with the database.");
    Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
    serviceRegistryBuilder.applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    Session session = sessionFactory.getCurrentSession();

    session.beginTransaction();
    Booking booking = (Booking) session.get(Booking.class, 3740456);
    System.out.println(booking.getSite());
}

}

Table description

desc BOOKING 
ID                             NOT NULL NUMBER                                                                                                                                                                                        
SITE                           VARCHAR2(10)                                                                                                                                                                                  

Query from log (this gives correct output)

 select
    booking0_.id as id1_0_0_,
    booking0_.site as site2_0_0_ 
from
    BOOKING booking0_ 
where
    booking0_.id=3740456;

exception

INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: 
select
    booking0_.id as id1_0_0_,
    booking0_.SITE as SITE2_0_0_ 
from
    BOOKING booking0_ 
where
    booking0_.id=?
May 22, 2015 8:22:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 904, SQLState: 42000
May 22, 2015 8:22:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00904: "BOOKING0_"."SITE": invalid identifier

May 22, 2015 8:22:07 AM org.hibernate.event.internal.DefaultLoadEventListener onLoad
INFO: HHH000327: Error performing load command :    org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at   org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
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.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1106)
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2587)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:991)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
at $Proxy7.get(Unknown Source)
at com.hibernate.demo.Main.main(Main.java:23)
Caused by: java.sql.SQLException: ORA-00904: "BOOKING0_"."SITE": invalid identifier

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:589)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1957)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:850)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2555)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2896)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:644)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:570)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
... 22 more

Aucun commentaire:

Enregistrer un commentaire