-->
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.  [ 1 post ] 
Author Message
 Post subject: org.hibernate.exception.SQLGrammarException: Could not execu
PostPosted: Thu Jul 05, 2007 1:51 pm 
Newbie

Joined: Mon May 28, 2007 6:24 am
Posts: 11
Location: Bangalore
Scenario

My program is a Simple Swing program which asks the user to input a ProductID and parts for it. This program demonstrates Hibernate Set Mapping.

Hibernate version: 3.2.4

Mapping documents

Product.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="hibernate.Product" table="products">
   <id name="productId" column="productId" >
       <generator class="assigned"/>
   </id>

  <set name="parts">
     <key column = "productId" not-null="true" />
     <one-to-many class = "hibernate.Parts" />
  </set>
</class>
</hibernate-mapping>



Parts.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="hibernate.Parts" table="parts">
    <id name="partId" column="partId" >
       <generator class="assigned"/>
     </id>
     <property name="partName" column="partName" />
  </class>
</hibernate-mapping>



Code



Code:
package hibernate;

import java.util.HashSet;
import java.util.Set;

import javax.swing.JOptionPane;

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

public class ProductAppln {
   public static void main(String[] args) {
       Session session = null;
       Transaction tx = null;
       Product product = null;
       String choice = null;
       int count = 0;
       try{
          SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          session =sessionFactory.openSession();
          do{
             choice = JOptionPane.showInputDialog(null, "Do you want to Insert Data for Products (Y/N)");
             if (choice.equalsIgnoreCase("Y")){
                String productId = JOptionPane.showInputDialog(null, "Enter the Product ID (Number)");
                int prodId = Integer.parseInt(productId);
                String noOfParts = JOptionPane.showInputDialog(null, "Enter the number of parts for " +
                                                             "product id : "+productId);
                int no_parts = Integer.parseInt(noOfParts);
                Set parts_set = new HashSet();
                for(int i = 0; i< no_parts ; i++){
                   String partId = JOptionPane.showInputDialog(null, "Enter part id (Number)");
                   int pId = Integer.parseInt(partId);
                   String partName = JOptionPane.showInputDialog(null, "Enter part Name");
                   Parts parts = new Parts();
                   parts.setPartId(pId);
                   parts.setPartName(partName);
                   parts_set.add(parts);
                   session.save(parts);
//                   session = sessionFactory.openSession();
                }
                tx = session.beginTransaction();
                product = new Product();
                product.setProductId(prodId);
                product.setParts(parts_set);
                session.save(product);
//                session =sessionFactory.openSession();
                tx.commit();
                count++;
          }else{
             return;
          }
          }while (!choice.equalsIgnoreCase("N"));
         
           System.out.println(count+ " Row(s) inserted into the Message Table");
          
       }
       catch(NumberFormatException e){
          e.printStackTrace();
       }catch(HibernateException e){
          System.out.println("Entered Duplicate Product id : "+product.getProductId());
          JOptionPane.showMessageDialog(null, "Entered Duplicate Product id : "+product.getProductId());
          e.printStackTrace();
       }
       catch(Exception e){
          e.printStackTrace();
       }finally{
          session.flush();
          session.close();
          System.exit(0);
       }
   }
}




Full stack trace of any exception that occurs:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: update parts set partName=? where partId=?
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at hibernate.ProductAppln.main(ProductAppln.java:71)
Caused by: java.sql.BatchUpdateException: ORA-00942: table or view does not exist

at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:441)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3377)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 6 more


Name and version of the database you are using:

Oracle 9i

Configuration File

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.url">jdbc:oracle:thin:@localhost:1521:bob</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="hibernate.connection.password">tiger</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
      <property name="show_sql">true</property>
      <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping resource="Message.hbm.xml" />
        <mapping resource="Parts.hbm.xml" />
        <mapping resource="Product.hbm.xml" />

      </session-factory>
</hibernate-configuration>
[/b]

_________________
http://www.hibernate-tutorial.com
http://www.spring-hibernate.com
http://www.spring-tutorial.com


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

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.