postgresql - 目标表未正确更新

标签 postgresql hibernate spring-mvc jsp

我用过:

spring mvc-version:4.3.7 postgres:9.5 Tool:STS

我想更新我的数据库。但是在表单中输入后,当我点击提交按钮时 - 它显示:

Request processing failed; nested exception is org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException: Object of class [com.asha.farmvill.model.Division] with identifier [0]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.asha.farmvill.model.Division#0]

这是我的 Controller :

@RequestMapping("/edit/{id}")
    public String editDivision(@PathVariable("id") int id, Model model) {
        model.addAttribute("division", this.divisionService.getdivisionById(id));
        return "editdivision";
    }

    @PostMapping(value="/updatedivision")
     public String updateEmployee(@ModelAttribute("division")Division division, ModelMap model) {

        this.divisionService.updatedivision(division);
                return "divisionlist";
    }

DAO 如下:

 public void updatedivision(Division p) {
        Session session = this.sessionFactory.getCurrentSession();
        session.update(p);

        System.out.println("Division updated successfully, Division Details=" + p.getName());

    }

这是我的 servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="org.postgresql.Driver" />
        <beans:property name="url"
            value="jdbc:postgresql://localhost:5432/firmvilldb" />
        <beans:property name="username" value="postgres" />
        <beans:property name="password" value="root" />
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate5AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.asha.farmvill.model.Division</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

   <beans:bean id="divisionDao" class="com.asha.farmvill.dao.DivisionDaoImp">
        <beans:property name="sessionFactory" ref="hibernate5AnnotatedSessionFactory" />
    </beans:bean>
    <beans:bean id="divisionService" class="com.asha.farmvill.service.DivisionServiceImp">
        <beans:property name="divisionDao" ref="divisionDao"></beans:property>
    </beans:bean>
    <context:component-scan base-package="com.asha.farmvill" />

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="hibernate5AnnotatedSessionFactory" />
    </beans:bean>
</beans:beans>

我该如何解决这个问题?

最佳答案

在下面的方法顶部添加 @Transactional 注释。

@Transactional
public void updatedivision(Division p) {
     Session session = this.sessionFactory.getCurrentSession();
     session.update(p);
     System.out.println("Division updated successfully, Division Details=" + p.getName());
}

引用Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

关于postgresql - 目标表未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52955278/

相关文章:

java - Hibernate 不提交数据

HibernateSystemException : Unknown entity: org. hibernate.collection.PersistentSet;

java - DataTable服务器端处理的数据结构

java - Spring MVC 网络应用国际化

mysql - 查询 A 喜欢 B,B 喜欢 C,两个分离度

sql - 从 bash 创建 postgresql 时出错 - 安装和导入

postgresql - 如何确定 upsert 是否是 PostgreSQL 9.5+ UPSERT 的更新?

java - Hibernate HQL JOIN - 获取的关联不存在

java - 从 `@WebFluxTest` 迁移到 `@SpringBootTest` 后,集成测试中连接被拒绝

python - 搭建学习平台——将练习与类(class)联系起来——单字段还是外键?