-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to fix org.hibernate.exception.SQLGrammarException
PostPosted: Thu Oct 08, 2009 6:09 am 
Newbie

Joined: Wed Oct 07, 2009 2:05 pm
Posts: 5
Dear All,

I am writting a small hibernante program.I am getting exception while running the Main.java.
Below given the code and exception.

Please suggest how i fix this error.

Main.java
Code:
package com.hibernate.student;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hibernate.util.HibernateUtil;

public class Main {

   public static void main(String[] args) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      try {
         transaction = session.beginTransaction();
         Address address = new Address("OMR Road", "Chennai", "TN", "600097");
         // By using cascade=all option the address need not be saved
         // explicitly when the student object is persisted the address will
         // be automatically saved.
         // session.save(address);
         Student student1 = new Student("Eswar", address);
         Student student2 = new Student("Joe", address);
         session.save(student1);
         session.save(student2);
         transaction.commit();
      } catch (HibernateException e) {
         transaction.rollback();
         e.printStackTrace();
      } finally {
         session.close();
      }

   }

}


Address.java
Code:
package com.hibernate.student;


/**
* This class contains the student's address
*          details.
*/
public class Address implements java.io.Serializable {

   private long addressId;
   private String street;
   private String city;
   private String state;
   private String zipcode;

   public Address() {
   }

   public Address(String street, String city, String state, String zipcode) {
      this.street = street;
      this.city = city;
      this.state = state;
      this.zipcode = zipcode;
   }

   public long getAddressId() {
      return this.addressId;
   }

   public void setAddressId(long addressId) {
      this.addressId = addressId;
   }

   public String getStreet() {
      return this.street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getCity() {
      return this.city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getState() {
      return this.state;
   }

   public void setState(String state) {
      this.state = state;
   }

   public String getZipcode() {
      return this.zipcode;
   }

   public void setZipcode(String zipcode) {
      this.zipcode = zipcode;
   }

}




Student.java
Code:
package com.hibernate.student;


/**
* This class contains student details.
*/
public class Student implements java.io.Serializable {

   private long studentId;
   private String studentName;
   private Address studentAddress;

   public Student() {
   }

   public Student(String studentName, Address studentAddress) {
      this.studentName = studentName;
      this.studentAddress = studentAddress;
   }

   public long getStudentId() {
      return this.studentId;
   }

   public void setStudentId(long studentId) {
      this.studentId = studentId;
   }

   public String getStudentName() {
      return this.studentName;
   }

   public void setStudentName(String studentName) {
      this.studentName = studentName;
   }

   public Address getStudentAddress() {
      return this.studentAddress;
   }

   public void setStudentAddress(Address studentAddress) {
      this.studentAddress = studentAddress;
   }

}


HibernateUtil.java
Quote:
package com.hibernate.util;

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

public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}


Address.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.hibernate.student.Address" table="ADDRESS">
      <meta attribute="class-description">This class contains the student's address
         details.</meta>
      <id name="addressId" type="long" column="ADDRESS_ID">
         <generator class="native" />
      </id>
      <property name="street" column="ADDRESS_STREET" type="string" length="250" />
      <property name="city" column="ADDRESS_CITY" type="string" length="50" />
      <property name="state" column="ADDRESS_STATE" type="string" length="50" />
      <property name="zipcode" column="ADDRESS_ZIPCODE" type="string" length="10" />
   </class>
</hibernate-mapping>


Student.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.hibernate.student.Student" table="STUDENT">
        <meta attribute="class-description">This class contains student details.</meta>
        <id name="studentId" type="long" column="STUDENT_ID">
            <generator class="native" />
        </id>
        <property name="studentName" type="string" length="100" not-null="true" column="STUDENT_NAME" />
        <many-to-one name="studentAddress" class="com.hibernate.student.Address" column="STUDENT_ADDRESS" cascade="all" not-null="true" />
    </class>
</hibernate-mapping>


hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@//localhost:1521/sumanta</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="connection.password">tiger</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create-drop</property>
        <mapping resource="com/hibernate/student/Student.hbm.xml"/>
        <mapping resource="com/hibernate/student/Address.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Code:
CREATE TABLE STUDENT(
STUDENT_ID  LONG,
STUDENT_NAME VARCHAR2(100) NOT NULL,
STUDENT_ADDRESS  VARCHAR2(50) NOT NULL
)


CREATE TABLE ADDRESS(
ADDRESS_ID    LONG,
ADDRESS_STREET    VARCHAR2(250),
ADDRESS_CITY      VARCHAR2(50),
ADDRESS_STATE     VARCHAR2(50),
ADDRESS_ZIPCODE   VARCHAR2(10));


Execption I am getting while running the Main.java

Hibernate: insert into ADDRESS (ADDRESS_ID, ADDRESS_STREET, ADDRESS_CITY, ADDRESS_STATE, ADDRESS_ZIPCODE) values (null, ?, ?, ?, ?)
Hibernate: call identity()
org.hibernate.exception.SQLGrammarException: could not retrieve generated id after insert: [com.hibernate.student.Address]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:93)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:534)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:526)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:291)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:239)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
at org.hibernate.engine.Cascade.cascade(Cascade.java:153)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:454)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
at com.hibernate.student.Main.main(Main.java:23)
Caused by: java.sql.SQLException: ORA-06576: not a valid function or procedure name

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:573)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1889)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:1093)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:2047)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1940)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2707)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:589)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:527)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:79)
... 31 more



Thanks for your time.
Regards,
Sumanta


Top
 Profile  
 
 Post subject: Re: How to fix org.hibernate.exception.SQLGrammarException
PostPosted: Thu Oct 08, 2009 7:27 am 
Newbie

Joined: Wed Sep 30, 2009 8:28 am
Posts: 10
change the dialect property as

<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>


Top
 Profile  
 
 Post subject: Re: How to fix org.hibernate.exception.SQLGrammarException
PostPosted: Thu Oct 08, 2009 9:26 am 
Newbie

Joined: Wed Oct 07, 2009 2:05 pm
Posts: 5
Thank you so much kanthan8 for the information.Now it is working properly.

Regards,
Sumanta


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.