Hibernate一对一映射错误org.hibernate.exception.ConstraintViolationException:无法执行JDBC批处理更新

时间:2015-12-18 10:49:35

标签: java hibernate

我正在对一对一映射记录执行删除操作,但它给了我错误

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
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:189)
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.dao.VitalService.removeVital(VitalService.java:126)
at org.Webapp.resource.VitalsResource.removeVital(VitalsResource.java:52)
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.JavaResourceMethodDispatcherProvider$VoidOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:141)
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 delete from Vitals where vitalId='50' 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

患者类:

@Entity
@Table(name = "Patient")
public class Patient {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int patientId;
private String firstName;
private String lastName;
private int age;
private String cnic;
private String contactNumber;
private String homeNumber;
private String country;
private String city;
private String town;
private String streetNo;
private String houseNo;
private String email;
private String username;
private String password;


public int getPatientId() {
    return patientId;
}
public void setPatientId(int patientId) {
    this.patientId = patientId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public String getCnic() {
    return cnic;
}
public void setCnic(String cnic) {
    this.cnic = cnic;
}
public String getContactNumber() {
    return contactNumber;
}
public void setContactNumber(String contactNumber) {
    this.contactNumber = contactNumber;
}
public String getHomeNumber() {
    return homeNumber;
}
public void setHomeNumber(String homeNumber) {
    this.homeNumber = homeNumber;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getTown() {
    return town;
}
public void setTown(String town) {
    this.town = town;
}
public String getStreetNo() {
    return streetNo;
}
public void setStreetNo(String streetNo) {
    this.streetNo = streetNo;
}
public String getHouseNo() {
    return houseNo;
}
public void setHouseNo(String houseNo) {
    this.houseNo = houseNo;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public int getId(){
    return patientId;
}

public Patient getPatient(){
    return this;
}
}

Vitals类:

@Entity
@Table(name = "Vitals")
public class Vitals {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int vitalId;
private float temperature;
private  int heartBeat;
private int respiratoryRate;
private int suger;
private int bp;
@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="patientId")
private Patient patient;

public int getVitalId() {
    return vitalId;
}
public void setVitalId(int vitalId) {
    this.vitalId = vitalId;
}
public int getBp() {
    return bp;
}
public void setBp(int bp) {
    this.bp = bp;
}
public float getTemperature() {
    return temperature;
}
public void setTemperature(float temperature) {
    this.temperature = temperature;
}
public int getHeartBeat() {
    return heartBeat;
}
public void setHeartBeat(int heartBeat) {
    this.heartBeat = heartBeat;
}
public int getRespiratoryRate() {
    return respiratoryRate;
}
public void setRespiratoryRate(int respiratoryRate) {
    this.respiratoryRate = respiratoryRate;
}
public int getSuger() {
    return suger;
}
public void setSuger(int suger) {
    this.suger = suger;
}
public Patient getPatient() {
    return patient;
}
public void setPatient(Patient patient) {
    this.patient = patient;
}
}

患者和重要对象类:

public class PatientVital {
private Vitals vitals;
private Patient patient;
public Vitals getVitals() {
    return vitals;
}
public void setVitals(Vitals vitals) {
    this.vitals = vitals;
}
public Patient getPatient() {
    return patient;
}
public void setPatient(Patient patient) {
    this.patient = patient;
}
}

服务类:

public class VitalService {

private SessionFactory sessionFactory = null;
public void removeVital(int id){
    try{
        sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Vitals vtl = new Vitals();
        vtl = (Vitals)session.get(Vitals.class, id);
        session.delete(vtl);
        session.getTransaction().commit();
        session.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }
}
}

资源类:

@Path("vitals")
public class VitalsResource {
VitalService vitalService = new VitalService();
@DELETE
@Path("{vitalId}")
@Produces(MediaType.APPLICATION_JSON)
public void removeVital(@PathParam("vitalId") int id){
    vitalService.removeVital(id);
}
}

数据库中有记录,但它给了我这个错误。任何人都可以告诉我,我在这里做错了什么。我将感恩:)

2 个答案:

答案 0 :(得分:0)

删除Patient时,我认为您不需要删除Vitals,因此请勿使用cascade=CascadeType.ALL

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="patientId")
private Patient patient;

答案 1 :(得分:0)

您有2个选项。

选项1:

更改级联类型。

@OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinColumn(name="patientId")
private Patient patient;

但请记住,当您要保存Vital时,您必须先保存Patient

Patient p = new Patient();
p.setFirstName("Patient");
....    

Vitals v1 = new Vitals();
v1.setPatient(p);
....    

Vitals v2 = new Vitals();
v2.setPatient(p);
....

session.save(p); // This is needed because cascade won't happen on persist
session.save(v1);
session.save(v2);

选项2:(这可能是最适合你的)

如果您不想更改级联类型,

@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="patientId")
private Patient patient;

然后,从服务方法

中删除Vital,如下所示
public void removeVital(int id){
    try{
        sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();


        Vitals vtl = (Vitals)session.get(Vitals.class, id);
        vtl.setPatient(null); // Patient may have other vitals.
        session.delete(vtl);

        session.getTransaction().commit();
        session.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }
}

希望这有帮助。