0

尝试从控制器更新实体时,我不断收到此异常。请放心,已经对这个例外进行了彻底的研究,但在我的情况下,这些建议都没有奏效。

我正在尝试通过调用 hibernate 来更新医生实体session.update(entity)

Doctor model:
    @Entity
@Table(name = "tbl_doctor")
public class Doctor {

private Long id;
private PersonInfo personInfo;
private String licenseNumber;
private User user;
private String specialization;
private String employmentStatus;
private String suffix;
private List<Patient> patients;


//
// @OneToMany
// @JoinTable(name = "Doctor_Appointment", joinColumns = { @JoinColumn(name
// = "doctor_id") }, inverseJoinColumns = { @JoinColumn(name = //
// "appointment_id") })
// private List<Appointment> appointments;

public Doctor() {
    super();
    user = new User();
    personInfo = new PersonInfo();
}

@Override
public String toString() {
    String patients= "";
    for(Patient p: this.patients) {
        patients += p.toString();
    }

    return "Doctor [id=" + id + ", personInfo=" + personInfo
            + ", licenseNumber=" + licenseNumber + ", user=" + user
            + ", specialization=" + specialization + ", employmentStatus="
            + employmentStatus + ", suffix=" + suffix + ", patients="
            + patients + "]";
}

@Column(name = "fld_license_number")
public String getLicenseNumber() {
    return licenseNumber;
}

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "key_user")
public User getUser() {
    return user;
}

@Column(name = "fld_specialization")
public String getSpecialization() {
    return specialization;
}

@Column(name = "fld_employment_status")
public String getEmploymentStatus() {
    return employmentStatus;
}

@Column(name = "fld_suffix")
public String getSuffix() {
    return suffix;
}

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "key_patient")
@Fetch(value = FetchMode.SUBSELECT)
public List<Patient> getPatients() {
    return patients;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "key_doctor")
public Long getId() {
    return id;
}

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "key_person_info")
public PersonInfo getPersonInfo() {
    return personInfo;
}
//setters

}

个人信息仅包括名字、姓氏、性别、生日等生物数据。

控制器

@RequestMapping(value="/update_doctor.it", method=RequestMethod.POST)
public String updateDoctor(HttpSession session, @ModelAttribute("doctor") Doctor     doctor, Model model, @RequestParam("id") long adminId) {
        System.err.println("update doctor controller");
        String username = session.getAttribute("user").toString();
        session.setAttribute("user", username);

        doctor.getUser().setEnabled(true);
        doctor.getPersonInfo().setDateModified(LocalDate.now());
        doctorDao.updateDoctor(doctor);
        return "redirect:/view_doctor_profile.it?id=" + doctor.getId();
    }

道实现

@Override
public void updateDoctor(Doctor doctor) {
    Session session = sessionFactory.getCurrentSession();
    session.update(doctor);
}

JSP

form:form class="docForm" method="post" commandName="doctor" action="update_doctor.it?id=${adminUser.getId() }" >
    <section>
        <header>

            <h3>Doctor Information</h3>
        </header>
    <form:hidden path="id" value="${doctor.id}" />

    <ul class="fields">
        <li><label>First Name</label>:<form:input value="${doctor.getPersonInfo().getFirstName()}" path="personInfo.firstName" type="text" required="true" /></li>
        <li><label>Last Name</label>:<form:input value="${doctor.getPersonInfo().getLastName()}" path="personInfo.lastName" type="text" required="true" /></li>
        <li><label>Suffix</label>:<form:input value="${doctor.getSuffix() }" path="suffix" /></li>
        <li><label>License Number</label>:<form:input value="${ doctor.getLicenseNumber() }" path="licenseNumber" /></li>
        <li><label>Occupation</label>:<form:input value="${doctor.getPersonInfo().getOccupation() }" path="personInfo.occupation"  /></li>
        <li><label>Specialization</label>:<form:input value="${ doctor.getSpecialization() }" path="specialization" required="true"/></li>
        <li><label>Date of Birth</label>:<input value="${doctor.getPersonInfo().getDateOfBirth() }" name="personInfo.dateOfBirth" type="text" id="datepicker" readonly/></li>
        <li><label>Gender</label>:
               <label>Male</label><form:radiobutton path="personInfo.sex" value="male"/>
                <label>Female</Label><form:radiobutton path="personInfo.sex" value="female"/>
        </li>
        <li><label>Phone Number</label>:<form:input value="${ doctor.getPersonInfo().getContacts().get(0).getPhoneNumber() }" path="personInfo.contacts[0].phoneNumber"/></li>
        <li><label>Mobile Number</label>:<form:input value="${ doctor.getPersonInfo().getContacts().get(0).getMobileNumber() }" path="personInfo.contacts[0].mobileNumber" type="text" required="true"/></li>
        <li><label>E-mail Address</label>:<form:input value="${ doctor.getPersonInfo().getEmail() }" path="personInfo.email" type="text" required="true"/></li>
    </ul>
    </section>

    <section>
    <header><h3>Address</h3></header>
    <ul class="fields">
        <li><label>Address</label>:<form:input value="${ doctor.getPersonInfo().getAddresses().get(0).getAddress() }" path="personInfo.addresses[0].address"/></li>
        <li><label>City</label>:<form:input value="${ doctor.getPersonInfo().getAddresses().get(0).getCity() }" path="personInfo.addresses[0].city"/></li>
        <li><label>Province</label>:<form:input value="${ doctor.getPersonInfo().getAddresses().get(0).getProvince() }" path="personInfo.addresses[0].province"/></li>
        <li><label>Zip Code</label>:<form:input value="${ doctor.getPersonInfo().getAddresses().get(0).getZipCode() }" path="personInfo.addresses[0].zipCode" /></li>
    </ul>

</section>

<section>
    <header><h3>Account Details</h3></header>
    <ul class="fields">
        <li><label>Username</label>:<form:input value="${ doctor.getUser().getUsername() }" path="user.username" required="true"/></li>
        <li><label>Password</label>:<form:password value="${ doctor.getUser().getPassword() }" path="user.password" required="true"/></li>
    </ul>
</section>

<section>
    <header>
        <h3>Hospital Details</h3>
    </header>
    <ul class="fields">
        <li><label>Name</label>:<form:input value="${doctor.getPersonInfo().getCompanyName() }" path="personInfo.companyName"/></li>
        <li><label>Employment Status</label>:
             <label>Full-time</label><form:radiobutton path="employmentStatus" value="full-time"/>
             <label>Part-time</Label><form:radiobutton path="employmentStatus" value="part-time"/>
        </li>
        <li><label>Phone Number</label>:<form:input value="${ doctor.getPersonInfo().getContacts().get(1).getPhoneNumber()}" path="personInfo.contacts[1].phoneNumber"/></li>
        <li><label>Work Mobile Number</label>:<form:input value="${ doctor.getPersonInfo().getContacts().get(1).getPhoneNumber()}" path="personInfo.contacts[1].mobileNumber" /></li>
        <li><label>Address</label>:<form:input value="${ doctor.getPersonInfo().getAddresses().get(1).getAddress() }" path="personInfo.addresses[1].address" /></li>
        <li><label>City</label>:<form:input value="${ doctor.getPersonInfo().getAddresses().get(1).getCity() }" path="personInfo.addresses[1].city"/></li>
        <li><label>Province</label>:<form:input value="${ doctor.getPersonInfo().getAddresses().get(1).getProvince() }" path="personInfo.addresses[1].province" /></li>     
        <li><label>Zip Code</label>:<form:input value="${ doctor.getPersonInfo().getAddresses().get(1).getZipCode() }" path="personInfo.addresses[1].zipCode" />

    </ul>
</section>  


        <section>
            <ul class="btnForm">
                <li><span class="btn"><input type="submit"
                        value="Save" class="btnS"></span></li>
                <li><span class="btn"><input type="button"
                        value="Cancel" class="btnCancel" onClick="viewPotentialsList()"></span></li>
            </ul>

错误

    SEVERE: Servlet.service() for servlet [emrMVC] in context with path [/emr] threw exception [Request processing failed; nested exception is     org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1] with root cause
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    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 org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:657)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy27.updateDoctor(Unknown Source)
    at com.ust.emr.controller.admin.EditDoctorController.updateDoctor(EditDoctorController.java:56)
    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.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

1 回答 1

0

我想到了。许多类或实体都依赖于医生,即我正在更新的实体。为了使批量更新成功执行,我必须在 jsp 上放置一个,获取依赖类的所有 id。谢谢!

于 2014-07-30T20:07:32.587 回答