SSH项目中,Service层获取对象,更改属性值后,MySQL中对应项也发生变化是什么原因?

前言

前几天下午做项目的时候发现了这个问题,当时把更改属性值的动作放到了Controller层了,今天下午刚好有时间就查询了一下。

测试代码

  1. Hibernate配置如下:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.connection.autocommit">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.gp.mujingyuan.pojo</value>
            </list>
        </property>
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <!-- 定义事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

2.DAO层代码

    public HandStrings getHandStringsById(int handStringsId) {
        HandStrings handStrings = hibernateTemplate.get(HandStrings.class,handStringsId);
        return handStrings;
    }

3.Service层代码

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    HandStringsDAO handStringsDAO;

    public HandStrings getXXXById(Integer hsId) {

        HandStrings handStrings = handStringsDAO.getHandStringsById(hsId);
        handStrings.setTitle("XXXXXXXXXXXXXXXXXXXXXXX");

        return handStrings;
    }
}

4.Controller层代码

    @RequestMapping(value = "/cookie",method = RequestMethod.GET)
    public String cookieTest(){

        testService.getXXXById(4);

        return "cookie";
    }

5.数据库数据运行前后对比
图片描述
图片描述
由以上两图对比,可知handStrings.setTitle("XXXXXXXXXXXXXXXXXXXXXXX");发生作用

事后分析

1.Hibernate的AutoCommit

最开始考虑的Hibernate的AutoCommit,后来测试了一下,添加配置项

<prop key="hibernate.connection.autocommit">true</prop>

无论设置成true还是false,并不能达到预期效果

2.flush Mode

之后在StackOverflow上看到一个帖子How can I globally set FlushMode for Hibernate 4.3.5.Final with Spring 4.0.6?,然后按照上面的解释进行了测试:

web.xml添加代码

  <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>com.gp.mujingyuan.filter.AutoFlushOpenSessionInViewFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

AutoFlushOpenSessionInViewFilter复写

public class AutoFlushOpenSessionInViewFilter extends OpenSessionInViewFilter {

    protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
        try {
            Session session = sessionFactory.openSession();
            session.setFlushMode(FlushMode.COMMIT); // This line changes the default behavior
            return session;
        } catch (HibernateException ex) {
            throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
        }
    }

}

结果还是不成功!

结语

现在还在查找原因以及解决方案

更新时间

2017-05-18

阅读 3.8k
2 个回答

不是太理解的你的问题,是不是期望在Controller中调用了handStrings.setTitle("XXXXXXXXXXXXXXXXXXXXXXX");,但是不需要自动的同步到数据库中去?

可以通过设置FlushModeMANUAL

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题