我从数据库中获取一个@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
我的密码是
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;
}}
结合任命类,医生和患者对象:
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;
}}
道课:
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文件:
<?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>
我要感谢你:)
最佳答案
该错误是由表名Central Appointment
中的空格字符引起的。如果您确实要使用带有空格的表名,则应使用反引号将其引起来:
@Table(name = "`Central Appointment`")
但是请记住:直接在数据库中执行SQL查询时,您还必须使用类似的引用技巧(对于Postgres,它是双引号-
select * from "Central Appointment" where ...
)。因此,不使用此类奇特的表名,而用下划线代替空格字符可能会更方便,也更便于同事使用:@Table(name = "Central_Appointment")