-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: SQLGrammarException: Could not execute JDBC batch update
PostPosted: Wed Jan 07, 2009 2:35 pm 
Newbie

Joined: Sun Oct 05, 2008 10:44 am
Posts: 7
my data base table contains 4 columns

1.docid.2.first.3.middle.4.last

my hibernate config xml is given below
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
  PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
   <property name="hibernate.connection.url">
      jdbc:postgresql://localhost/newgenlib
   </property>
   <property name="hibernate.connection.driver_class">
      org.postgresql.Driver
   </property>
   <property name="hibernate.connection.username">
      postgres
   </property>
   <property name="hibernate.connection.password">
      girish
   </property>
   <property name="hibernate.connection.pool_size">
      0
   </property>
   <!--<property name="hibernate.jdbc.batch_size">0</property>-->
   <property name="hibernate.dialect">
      org.hibernate.dialect.PostgreSQLDialect
   </property>
   <property name="hibernate.show_sql">
      true
   </property>
   <property name="connection.autocommit">
      false
   </property>
<!-- "Import" the mapping resources here -->
<mapping resource="InsertBean.hbm.xml"/>
</session-factory>
</hibernate-configuration>

mapping xml

Insertbean.xml

<?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="InsertBean" table="InsertBean">
     <id column="docid" unsaved-value="-1" type="integer">
       <generator class="sequence">
         <param name="sequence">sequence_id</param>
      </generator>
     </id>
     <property name="First" column="first" type="string"/>
    <property name="Middle" column="middle" type="string"/>
    <property name="Last" column="last" type="string"/>
   </class>
</hibernate-mapping>



my pojo class is given below


public class InsertBean
{
   public String First;
   public String Middle;
   public String Last;
   public InsertBean()
   {
      
   }
   public InsertBean(String First,String Middle,String Last)
   {
      this.First=First;
      this.Middle=Middle;
      this.Last=Last;
   }

   public String getFirst()
   {
      return this.First;
   }
   public void setFirst(String First)
   {
      this.First=First;
   }
   public String getMiddle()
   {
      return this.Middle;
   }
   public void setMiddle(String Middle)
   {
         this.Middle=Middle;
   }
   public String getLast()
   {
      return this.Last;
   }
      public void setLast(String Last)
   {
      this.Last= Last;
   }

}

MyServlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet
{
   public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
   {
       
       System.out.println("INSIDE SERVLET");
       HibernateInsert hi=new HibernateInsert();
       hi.insert();
   
   
   }

}


HibernateInsert.java

import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateInsert
{
   public void insert(){
      
         SessionFactory factory =
      new Configuration().configure().buildSessionFactory();
      Session session = factory.openSession();
      session.beginTransaction();
      InsertBean m1 = new InsertBean();
      m1.setFirst("new");
      m1.setMiddle("gen");
      m1.setLast("lib");
      session.save(m1);
      session.getTransaction().commit();
      session.close();
      }
      

}



when i execute this web application i am getting this exception please help me

org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 6:27 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Could you please paste your full stacktrace?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 7:13 am 
Newbie

Joined: Sun Oct 05, 2008 10:44 am
Posts: 7
thank you for the reply i am sry but i am not getting any stack trace i am using sun application server i am begginner at java and enterprise technologies however i have managed to get the SQL query that hibernate is generating

insert into InsertBean(first,middle,last ) values (?,?,?);

soon i will the exception stack trace also
thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 8:53 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
i think u set batch_size to 0(Zero).
First set the jdbc.batch_size property to any number b/w 10 to 50.

if u r saving & update more no. of objects into DB. then check the loop index for batch size if it reaches that flush() and clear() the session.

Actually batch processing used for large number of objects accessing. for eg., if we r going to save 5000 users data without batch in middle of 1000 or some object the JVM show error Heap Memory Out exception.

for eg., how to use batch process for saving users object .

set jdbc.batch_size=30 in hibernate.cfg.xml file


for(int i=0;i<users.length;i++)
{
Users u =new Users();
u.setId(i);
....
if(i%30==0){
session.flush();
session.clear();
}
session.save(u); // session.update(u);
}

In this way we will deal with batch processing. it same procedure used in update also.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 9:02 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I have not used generator class "sequence" yet, but the insert statement is missing the id-column.


What you should do is add an attribute docid to your entity.
Code:
public class InsertBean {
    private int docid;
   
    //these should normally be private
    public String First;
    public String Middle;
    public String Last;
...


Maybe this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 1:06 pm 
Newbie

Joined: Sun Oct 05, 2008 10:44 am
Posts: 7
Thnak you for your reply again both both the solutions did not work i am still getting the same exception


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 7:16 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Did you add get/set methods for docid too?

I use postgres too and
Code:
   <class name="data.User" table="USERS">
      <id name="id" type="int" column="ID">
         <generator class="native" />
      </id>

works fine for me. "native" should default to sequence on postgres, hibernate generated its own sequence when i set up the schema, you might have to do this manually.

As far as i understand your mapping you need an sequence named "sequence_id" in your datebase, does this exist?

When i create a new object in a session i see a SELECT for the next value from the sequence first, then an INSERT


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 1:08 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
try as i said.


if error comes paste ur new code,hbm and stack trace.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject: Re: SQLGrammarException: Could not execute JDBC batch update
PostPosted: Wed Jan 27, 2010 7:37 am 
Newbie

Joined: Wed Jan 27, 2010 7:26 am
Posts: 4
Location: Novi Sad, Serbia
hi!
My name is Kath and I've been reading this forum whenever I had problem.
But this time, I need to ask something about this theme, and I hope you will help...


anyway, Im working on my final graduate work, and I have a problem...
I worked on my college database, but they shut it down, and Im trying to fix everything on my computer now.
So I downloaded everything I need and tried to re-do hibernate settings...

I have this java class...
Code:
package model;

// Generated Dec 28, 2009 11:29:22 AM by Hibernate Tools 3.2.2.GA

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

/**
* Kb48302Sifarnikpisaca generated by hbm2java
*/
public class Kb48302Sifarnikpisaca implements java.io.Serializable {

   private String pisciId;
   private String pisciIme;
   private String pisciPrezime;
   private Set kb48302Artiklis = new HashSet(0);

   public Kb48302Sifarnikpisaca() {
   }

   public Kb48302Sifarnikpisaca(String pisciId) {
      this.pisciId = pisciId;
   }

   public Kb48302Sifarnikpisaca(String pisciId, String pisciIme,
         String pisciPrezime, Set kb48302Artiklis) {
      this.pisciId = pisciId;
      this.pisciIme = pisciIme;
      this.pisciPrezime = pisciPrezime;
      this.kb48302Artiklis = kb48302Artiklis;
   }

   public String getPisciId() {
      return this.pisciId;
   }

   public void setPisciId(String pisciId) {
      this.pisciId = pisciId;
   }

   public String getPisciIme() {
      return this.pisciIme;
   }

   public void setPisciIme(String pisciIme) {
      this.pisciIme = pisciIme;
   }

   public String getPisciPrezime() {
      return this.pisciPrezime;
   }

   public void setPisciPrezime(String pisciPrezime) {
      this.pisciPrezime = pisciPrezime;
   }

   public Set getKb48302Artiklis() {
      return this.kb48302Artiklis;
   }

   public void setKb48302Artiklis(Set kb48302Artiklis) {
      this.kb48302Artiklis = kb48302Artiklis;
   }

}


and this test
Code:
package model;

import javax.transaction.Transaction;

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

public class HibernateTest {

   /**
    * @param args
    */
   public static void main(String[] args) {
      Configuration cfg = new Configuration();
      cfg.configure("model/hibernate.cfg.xml");
      SessionFactory sf = cfg.buildSessionFactory();
      Session s = sf.openSession();
      
      org.hibernate.Transaction tx = s.beginTransaction();
      Kb48302Sifarnikpisaca pisac = new Kb48302Sifarnikpisaca();
      pisac.setPisciId("13");
      pisac.setPisciIme("Niki");
      pisac.setPisciPrezime("French");
      
      s.save(pisac);
      tx.commit();

   }

}


my hibernate file looks like this:
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">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">myPassword</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/diplomski483-02</property>
        <property name="hibernate.connection.username">kath</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
       <mapping resource="model/Kb48302Sifarnikpisaca.hbm.xml" />
    </session-factory>
</hibernate-configuration>



how can I get this to work??
Code:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
   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:266)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
   at model.HibernateTest.main(HibernateTest.java:27)
Caused by: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-02.kb48302_sifarnikpisaca (PISCI_IME, PISCI_PREZIME, PISCI_ID) values ('Niki', ' at line 1
   at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   ... 8 more




thanks in advance :)


Top
 Profile  
 
 Post subject: Re: SQLGrammarException: Could not execute JDBC batch update
PostPosted: Wed Jan 27, 2010 8:46 am 
Newbie

Joined: Wed Jan 27, 2010 8:10 am
Posts: 4
Kath,

Need you to post your hibernate mapping file.

Regards,
Praveen


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jan 27, 2010 9:02 am 
Newbie

Joined: Wed Jan 27, 2010 7:26 am
Posts: 4
Location: Novi Sad, Serbia
kath wrote:

my hibernate file looks like this:
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">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">myPassword</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/diplomski483-02</property>
        <property name="hibernate.connection.username">kath</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
       <mapping resource="model/Kb48302Sifarnikpisaca.hbm.xml" />
    </session-factory>
</hibernate-configuration>



I did :)
or I need to post something else?
anyway, my mapping file has more of
Code:
<mapping resource="model/Kb48302Sifarnikpisaca.hbm.xml" />

21 of them, but I deleted them here because they are not important for my HibernetTest.java
there I just try to insert into table that is not connected to others.

Thanks for trying to help :)


Top
 Profile  
 
 Post subject: Re: SQLGrammarException: Could not execute JDBC batch update
PostPosted: Wed Jan 27, 2010 9:49 am 
Newbie

Joined: Wed Jan 27, 2010 8:10 am
Posts: 4
I was talking about this file..
Kb48302Sifarnikpisaca.hbm.xml


Top
 Profile  
 
 Post subject: Re: SQLGrammarException: Could not execute JDBC batch update
PostPosted: Wed Jan 27, 2010 9:56 am 
Newbie

Joined: Wed Jan 27, 2010 7:26 am
Posts: 4
Location: Novi Sad, Serbia
Im sorry.
Not so bright this morning :D
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">
<!-- Generated Jan 26, 2010 1:13:52 PM by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
    <class name="model.Kb48302Sifarnikpisaca" table="kb48302_sifarnikpisaca" catalog="diplomski483-02">
        <id name="pisciId" type="string">
            <column name="PISCI_ID" length="15" />
            <generator class="assigned" />
        </id>
        <property name="pisciIme" type="string">
            <column name="PISCI_IME" length="25" />
        </property>
        <property name="pisciPrezime" type="string">
            <column name="PISCI_PREZIME" length="25" />
        </property>
    </class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject: Re: SQLGrammarException: Could not execute JDBC batch update
PostPosted: Thu Jan 28, 2010 2:54 am 
Newbie

Joined: Wed Jan 27, 2010 8:10 am
Posts: 4
Kath,

I created a test application with your artifacts and was able to successfully insert the record.

Your db table should be something like the one below

CREATE TABLE kb48302_sifarnikpisaca (
PISCI_ID varchar(15) NOT NULL,
PISCI_IME varchar(25) NULL,
PISCI_PREZIME varchar(25) NULL,
PRIMARY KEY(PISCI_ID)
)


The error you encountered seems to be a SQL syntax error: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-02.kb48302_sifarnikpisaca (PISCI_IME, PISCI_PREZIME, PISCI_ID) values ('Niki', ' at line 1


Top
 Profile  
 
 Post subject: Re: SQLGrammarException: Could not execute JDBC batch update
PostPosted: Thu Jan 28, 2010 2:56 am 
Newbie

Joined: Wed Jan 27, 2010 8:10 am
Posts: 4
Share your table structure and will see if the issue can be reproduced.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.