title: org.hibernate.exception.SQLGrammarException:Could not execute JDBC batch update
body: <p>I am geting a @OneToOne reletionship record from database and insert into another database using hibernate but having this error</p>
  
  <pre><code>Hibernate: select appointmen0_.appointmentId as appointm1_13_2_, appointmen0_.appointmentDay as appointm2_13_2_, appointmen0_.appointmentTime as appointm3_13_2_, appointmen0_.doctor_userId as doctor4_13_2_, appointmen0_.patient_patientId as patient5_13_2_, doctor1_.userId as userId2_0_, doctor1_.age as age2_0_, doctor1_.city as city2_0_, doctor1_.cnic as cnic2_0_, doctor1_.contactNumber as contactN5_2_0_, doctor1_.country as country2_0_, doctor1_.email as email2_0_, doctor1_.firstName as firstName2_0_, doctor1_.homeNumber as homeNumber2_0_, doctor1_.houseNo as houseNo2_0_, doctor1_.lastName as lastName2_0_, doctor1_.password as password2_0_, doctor1_.streetNo as streetNo2_0_, doctor1_.town as town2_0_, doctor1_.username as username2_0_, doctor1_.specialization as speciali1_4_0_, patient2_.patientId as patientId5_1_, patient2_.age as age5_1_, patient2_.city as city5_1_, patient2_.cnic as cnic5_1_, patient2_.contactNumber as contactN5_5_1_, patient2_.country as country5_1_, patient2_.email as email5_1_, patient2_.firstName as firstName5_1_, patient2_.homeNumber as homeNumber5_1_, patient2_.houseNo as houseNo5_1_, patient2_.lastName as lastName5_1_, patient2_.password as password5_1_, patient2_.streetNo as streetNo5_1_, patient2_.town as town5_1_, patient2_.username as username5_1_ from AppointmentClass appointmen0_ left outer join Doctor doctor1_ on appointmen0_.doctor_userId=doctor1_.userId left outer join Patient patient2_ on appointmen0_.patient_patientId=patient2_.patientId where appointmen0_.appointmentId=?
  Hibernate: insert into Patient (age, city, cnic, contactNumber, country, email, firstName, homeNumber, houseNo, lastName, password, streetNo, town, username, patientId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  Hibernate: insert into Doctor (age, city, cnic, contactNumber, country, email, firstName, homeNumber, houseNo, lastName, password, specialization, streetNo, town, username, userId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  Hibernate: insert into Central Appointment (day, doctor_userId, patient_patientId, time, appointmentId) values (?, ?, ?, ?, ?)
  
  
  org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
  at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
  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:268)
  at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
  at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
  at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
  at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
  at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
  at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
  at com.ehr.admin.services.CentralAppointmentPatientService.transferAppointment(CentralAppointmentPatientService.java:90)
  at org.Webapp.admin.CentralizedAppointmentResource.addNewAppointment(CentralizedAppointmentResource.java:20)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
  at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:143)
  at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:160)
  at org.glassfish.jersey.server.model.internal.VoidVoidDispatcherProvider$VoidToVoidDispatcher.doDispatch(VoidVoidDispatcherProvider.java:78)
  at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
  at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
  at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
  at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
  at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:303)
  at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
  at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
  at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
  at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
  at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
  at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source) 
  Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into Central Appointment (day, doctor_userId, patient_patientId, time, appointmentId) values ('10', '21', '20', '11', '22') was aborted.  Call getNextException to see the cause.
  at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2621)
  at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1837)
  at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2754)
  at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
  at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
  ... 51 more
  </code></pre>
  
  <p>My Code is </p>
  
  <pre><code>package com.ehr.admin;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.Id;
  import javax.persistence.OneToOne;
  import javax.persistence.Table;
  @Entity
  @Table(name = "Central Appointment")
  public class CentralAppointment {
  
  @Id
  private int appointmentId;
  private int day;
  private int time;
  @OneToOne(fetch = FetchType.EAGER)
  private CentralPatient patient;
  @OneToOne(fetch = FetchType.EAGER)
  private CentralDoctor doctor;
  public int getAppointmentId() {
      return appointmentId;
  }
  public void setAppointmentId(int appointmentId) {
      this.appointmentId = appointmentId;
  }
  public CentralPatient getPatient() {
      return patient;
  }
  public void setPatient(CentralPatient patient) {
      this.patient = patient;
  }
  public int getDay() {
      return day;
  }
  public void setDay(int day) {
      this.day = day;
  }
  public int getTime() {
      return time;
  } 
  public void setTime(int time) {
      this.time = time;
  }
  public CentralDoctor getDoctor() {
      return doctor;
  }
  public void setDoctor(CentralDoctor doctor) {
      this.doctor = doctor;
  }}
  </code></pre>
  
  <p>Combine Class of Appointment,Doctor and patient Object:</p>
  
  <pre><code>public class CentralPatientAppointment {
  private CentralAppointment appointment;
  private CentralPatient patient;
  private CentralDoctor doctor;
  
  public CentralAppointment getAppointment() {
      return appointment;
  }
  public void setAppointment(CentralAppointment appointment) {
      this.appointment = appointment;
  }
  public CentralPatient getPatient() {
      return patient;
  }
  public void setPatient(CentralPatient patient) {
      this.patient = patient;
  }
  public CentralDoctor getDoctor() {
      return doctor;
  }
  public void setDoctor(CentralDoctor doctor) {
      this.doctor = doctor;
  }}
  </code></pre>
  
  <p>Dao Class:</p>
  
  <pre><code>package com.ehr.admin.services;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import com.ehr.admin.CentralAppointment;
  import com.ehr.admin.CentralDoctor;
  import com.ehr.admin.CentralPatient;
  import com.ehr.dao.AppointmentPatientService;
  import com.ehr.data.AppointmentClass;
  public class CentralAppointmentPatientService {
  
  
  private SessionFactory sessionFactory = null;
  
  
  public void transferAppointment(){
      AppointmentPatientService app = new AppointmentPatientService();
      AppointmentClass apt = new AppointmentClass();
      apt = app.getParticularPatientAppointment(22);
  
      CentralAppointment cpt = new CentralAppointment();
      CentralDoctor doctor = new CentralDoctor();
      CentralPatient patient = new CentralPatient();
  
  
      cpt.setAppointmentId(apt.getAppointmentId());
      cpt.setDay(apt.getAppointmentDay());
      cpt.setTime(apt.getAppointmentTime());
  
  
  
  
  
      /**
       * Patient Object
       */
      patient.setId(apt.getPatient().getId());
      patient.setFirstName(apt.getPatient().getFirstName());
      patient.setLastName(apt.getPatient().getLastName());
      patient.setAge(apt.getPatient().getAge());
      patient.setCity(apt.getPatient().getCity());
      patient.setCnic(apt.getPatient().getCnic());
      patient.setContactNumber(apt.getPatient().getContactNumber());
      patient.setCountry(apt.getPatient().getCountry());
      patient.setEmail(apt.getPatient().getEmail());
      patient.setHomeNumber(apt.getPatient().getHomeNumber());
      patient.setHouseNo(apt.getPatient().getHouseNo());
      patient.setPassword(apt.getPatient().getPassword());
      patient.setStreetNo(apt.getPatient().getStreetNo());
      patient.setTown(apt.getPatient().getTown());
      patient.setUsername(apt.getPatient().getUsername());
  
      /**
       * Doctor Object
       */
      doctor.setUserId(apt.getDoctor().getUserId());
      doctor.setFirstName(apt.getDoctor().getFirstName());
      doctor.setLastName(apt.getDoctor().getLastName());
      doctor.setAge(apt.getDoctor().getAge());
      doctor.setCity(apt.getDoctor().getCity());
      doctor.setCnic(apt.getDoctor().getCnic());
      doctor.setContactNumber(apt.getDoctor().getContactNumber());
      doctor.setCountry(apt.getDoctor().getCountry());
      doctor.setEmail(apt.getDoctor().getEmail());
      doctor.setHomeNumber(apt.getDoctor().getHomeNumber());
      doctor.setHouseNo(apt.getDoctor().getHouseNo());
      doctor.setPassword(apt.getDoctor().getPassword());
      doctor.setStreetNo(apt.getDoctor().getStreetNo());
      doctor.setTown(apt.getDoctor().getTown());
      doctor.setUsername(apt.getDoctor().getUsername());
      doctor.setSpecialization(apt.getDoctor().getSpecialization());
  
      cpt.setPatient(patient);
      cpt.setDoctor(doctor);
  
      try{
          sessionFactory = new Configuration().configure("hibernate1.cfg.xml").buildSessionFactory();
          Session session = sessionFactory.openSession();
          session.beginTransaction();
          session.save(patient);
          session.save(doctor);
          session.save(cpt);
          session.getTransaction().commit();
          session.close();
          sessionFactory.close();
      }catch(Exception ex){
          ex.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>hibernate1.cfg.xml File:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;session-factory&gt;
  
      &lt;!-- Database connection settings --&gt;
      &lt;!-- &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt; --&gt;
  
      &lt;property name="connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
  
      &lt;!-- &lt;property name="connection.url"&gt;jdbc:mysql://174.143.132.195:3306/977266_EHR&lt;/property&gt;
      &lt;property name="connection.username"&gt;977266_admin&lt;/property&gt;
      &lt;property name="connection.password"&gt;EHr12345&lt;/property&gt; --&gt; 
  
      &lt;property name="connection.url"&gt;jdbc:postgresql://localhost:5432/Centralized Database&lt;/property&gt;
      &lt;property name="connection.username"&gt;postgres&lt;/property&gt;
      &lt;property name="connection.password"&gt;project&lt;/property&gt;
  
  
      &lt;!-- JDBC connection pool (use the built-in) --&gt;
      &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
      &lt;!-- SQL dialect --&gt;
      &lt;property name="dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
  
      &lt;!-- Disable the second-level cache  --&gt;
      &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
  
      &lt;!-- Echo all executed SQL to stdout --&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
  
      &lt;property name="hibernate.enable_lazy_load_no_trans"&gt;true&lt;/property&gt;
  
      &lt;property name="hibernate.default_catalog "&gt;Centralized Database &lt;/property&gt;
  
      &lt;!-- Drop and re-create the database schema on startup --&gt;
      &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
  
      &lt;!-- Names the annotated entity class --&gt;
      &lt;mapping class="com.ehr.admin.CentralPatient"/&gt;
      &lt;mapping class="com.ehr.admin.CentralAppointment"/&gt;
      &lt;mapping class="com.ehr.admin.CentralDoctor"/&gt;
      &lt;mapping class="com.ehr.admin.CentralReceptionist"/&gt;
  
  
  &lt;/session-factory&gt;
  </code></pre>
  
  <p></p>
  
  <p>I shall b thankful to u :)</p>