簡體   English   中英

休眠一對一映射錯誤org.hibernate.exception.ConstraintViolationException:無法執行JDBC批更新

[英]Hibernate one to one mapping error org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

我正在一對一映射記錄上執行刪除操作,但它給了我錯誤

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

重要等級:

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

數據庫中有記錄,但這給了我錯誤。 誰能告訴我我在這里做錯了什么。 我將很感激:)

我認為刪除重要Vitals時不需要刪除“ Patient ,所以不要使用cascade=CascadeType.ALL

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

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM