This week's giveaway is in the Groovy forum.
We're giving away four copies of Groovy Fundamentals video training course and have Ken Kousen on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate SQLGrammarException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply locked New topic

Hibernate SQLGrammarException

Saikat Gupta

Joined: Jun 07, 2007
Posts: 4

I am facing a problem when I try to run this code.

=================My Entity Class==================
@NamedNativeQuery(name = "Vehicle.byName", query = "select * from VEHICLE where VEHICLE_NAME = ?", resultClass=Vehicle.class)
public class Vehicle{
private static final long serialVersionUID = 1L;

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int vehicleId;

private String vehicleName;

//Getters and Setters......

==================My hibernate.cfg.xml is as below==================

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">*********</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="">update</property>

<!-- Names the annotated entity class -->
<mapping class="org.saikat.javaapps.hibernate.Vehicle"/>

=======My Service class========================

public class GetVehicle {

* @param args
public static void main(String[] args) {
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();

Query query = session.getNamedQuery("Vehicle.byName");
query.setString(0, "Porshe");
List<Vehicle> vehicleList = (List<Vehicle>)query.list();

for(Vehicle v : vehicleList){
System.out.println("Vehicle retrieved is " + v.getVehicleName());
My database structure is like below.


Now the problem is when I try to run the service method it throws an error like,


SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See for further details.
Hibernate: select * from VEHICLE where VEHICLE_NAME = ?
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(
at org.hibernate.exception.JDBCExceptionHelper.convert(
at org.hibernate.loader.Loader.doList(
at org.hibernate.loader.Loader.listIgnoreQueryCache(
at org.hibernate.loader.Loader.list(
at org.hibernate.loader.custom.CustomLoader.list(
at org.hibernate.impl.SessionImpl.listCustomQuery(
at org.hibernate.impl.AbstractSessionImpl.list(
at org.hibernate.impl.SQLQueryImpl.list(
at org.saikat.javaapps.service.GetVehicle.main(
Caused by: java.sql.SQLException: Column 'clazz_' not found.
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.ResultSet.findColumn(
at com.mysql.jdbc.ResultSet.getInt(
at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(
at org.hibernate.loader.Loader.getInstanceClass(
at org.hibernate.loader.Loader.instanceNotYetLoaded(
at org.hibernate.loader.Loader.getRow(
at org.hibernate.loader.Loader.getRowFromResultSet(
at org.hibernate.loader.Loader.doQuery(
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(
at org.hibernate.loader.Loader.doList(
... 7 more


Please, give me the expected solution.

Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3883

Please continue the discussion in your other thread. And please don't duplicate posts in the forums (CarefullyChooseOneForum).

[How to ask questions] [Twitter]
I agree. Here's the link:
subject: Hibernate SQLGrammarException