更新的查询无法在休眠中工作

时间:2018-10-24 07:36:38

标签: postgresql hibernate spring-mvc

我用过:

  

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

我的岛:

   @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void updatedivision(Division p) {
        Session session = this.sessionFactory.getCurrentSession();
         Query query = session.createQuery("update Division set name =:name where id = :id ");
         query.setParameter("name", p.getName());  
         query.setParameter("id", p.getId());
         int result = query.executeUpdate();    
         session.flush();

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

我想更新我的数据库。但是在表单中输入之后。但是当我运行代码时,正确接收到了更新的值(我已经通过打印该值进行了检查。)但是表未更新。

我的servlet-contet.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 -->

  <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 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:prop key="hibernate.connection.pool_size">1</beans:prop>
              <beans:prop key="hibernate.default-lazy">false</beans:prop>
                <beans:prop key="hbm2ddl.auto">update</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" />



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

但是它没有显示任何错误。 这是我的模特:

  @Entity
    @Table(name = "division")
    public class Division implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name="id")
        private Integer id;

        @Column(name="name")
        private String name;

我的桌子

    CREATE TABLE public.division
(
  id integer NOT NULL,
  name character varying(20) NOT NULL,
  CONSTRAINT division_pkey PRIMARY KEY (id)
)

2 个答案:

答案 0 :(得分:0)

解决方案1-

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void updatedivision(Division p) {
       Session session = this.sessionFactory.getCurrentSession();
       session.saveOrUpdate(p);
       session.flush();
}

解决方案2-

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void updatedivision(Division p) {
    Session session = this.sessionFactory.getCurrentSession();
    tx = session.beginTransaction();
    Query query = session.createQuery("update Division set name =:name where id = :id ");
    query.setParameter("name", p.getName());  
    query.setParameter("id", p.getId());
    int result = query.executeUpdate();
    if(result > 0){          
        System.out.println("Division updated successfully, Division Details=" + p.getName());
    }
    tx.commit();
}

答案 1 :(得分:0)

您可以像这样使用会话对象的saveOrUpdate方法来代替使用查询来更新表

session.saveOrUpdate(p);

如果您的用例不是编写查询,这将在表中创建新行或更新现有行。您无需在此处使用查询。