简体   繁体   English

org.hibernate.exception.SQLGrammarException:无法执行 JDBC 批量更新

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

I am geting a @OneToOne reletionship record from database and insert into another database using hibernate but having this error我从数据库中获取@OneToOne 关联记录并使用休眠插入到另一个数据库中,但出现此错误

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

My Code is我的代码是

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;
}}

Combine Class of Appointment,Doctor and patient Object:结合预约类别、医生和患者对象:

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;
}}

Dao Class:道类:

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();
    }
}
}

hibernate1.cfg.xml File: hibernate1.cfg.xml 文件:

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

<session-factory>

    <!-- Database connection settings -->
    <!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->

    <property name="connection.driver_class">org.postgresql.Driver</property>

    <!-- <property name="connection.url">jdbc:mysql://174.143.132.195:3306/977266_EHR</property>
    <property name="connection.username">977266_admin</property>
    <property name="connection.password">EHr12345</property> --> 

    <property name="connection.url">jdbc:postgresql://localhost:5432/Centralized Database</property>
    <property name="connection.username">postgres</property>
    <property name="connection.password">project</property>


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

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</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>


    <property name="hibernate.enable_lazy_load_no_trans">true</property>

    <property name="hibernate.default_catalog ">Centralized Database </property>

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

    <!-- Names the annotated entity class -->
    <mapping class="com.ehr.admin.CentralPatient"/>
    <mapping class="com.ehr.admin.CentralAppointment"/>
    <mapping class="com.ehr.admin.CentralDoctor"/>
    <mapping class="com.ehr.admin.CentralReceptionist"/>


</session-factory>

I shall b thankful to u :)我会感谢你 :)

Just try following steps:-只需尝试以下步骤:-

  1. In the hibernate.cfg.xml, add this property在 hibernate.cfg.xml 中,添加这个属性

    hibernate.default_catalog = MyDatabaseName hibernate.default_catalog = MyDatabaseName

  2. In the hibernate.reveng.xml, add all the table filters like this在 hibernate.reveng.xml 中,像这样添加所有表过滤器

    table-filter match-name="MyTableName" (just this, no catalog name here) table-filter match-name="MyTableName"(只是这个,这里没有目录名称)

  3. Regenerate hibernate code重新生成休眠代码

The error is caused by a space character in the table name Central Appointment .该错误是由表名Central Appointment的空格字符引起的。 If you really want to use a table name with a space in it, you should quote it using backticks:如果你真的想使用一个带有空格的表名,你应该使用反引号引用它:

@Table(name = "`Central Appointment`")

But remember: you will also have to use similar quoting tricks when executing SQL queries directly in the database (in case of Postgres it's double quotes - select * from "Central Appointment" where ... ).但请记住:在数据库中直接执行 SQL 查询时,您还必须使用类似的引用技巧(在 Postgres 的情况下,它是双引号 - select * from "Central Appointment" where ... )。 So it might be more convenient and also more colleague-friendly to not use such exotic table names and instead replace the space character with an underscore:因此,不使用这种异国情调的表名,而是用下划线替换空格字符可能更方便,也对同事更友好:

@Table(name = "Central_Appointment")

Remove space if you have added while mentioning the column name如果您在提及列名时添加了空格,请删除空格

@Column(name="PassengerName") @Column(name="乘客姓名")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.exception.SQLGrammarException:无法执行JDBC批更新 - org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update 嵌套的异常是org.hibernate.exception.SQLGrammarException:无法执行查询 - nested exception is org.hibernate.exception.SQLGrammarException: could not execute query 如何解决 org.hibernate.exception.SQLGrammarException: 无法执行查询 - How to solve org.hibernate.exception.SQLGrammarException: could not execute query RuntimeException:org.hibernate.exception.SQLGrammarException:无法执行查询 - RuntimeException: org.hibernate.exception.SQLGrammarException: could not execute query org.hibernate.exception.SQLGrammarException:无法执行查询 - org.hibernate.exception.SQLGrammarException: could not execute query org.hibernate.exception.SQLGrammarException:无法执行语句 - org.hibernate.exception.SQLGrammarException: could not execute statement org.hibernate.exception.SQLGrammarException:无法再次执行查询 - org.hibernate.exception.SQLGrammarException: could not execute query again org.hibernate.exception.SQLGrammarException:无法执行语句 - org.hibernate.exception.SQLGrammarException: could not execute statement org.springframework.web.util.NestedServletException:嵌套的异常是org.hibernate.exception.SQLGrammarException:无法执行语句 - org.springframework.web.util.NestedServletException: nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement 引起:org.hibernate.exception.SQLGrammarException: JDBC 异常执行 SQL - Caused by: org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM