@Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext( "spring_dao.xml"); SessionFactory sessionfactory = (SessionFactory) ac .getBean("sessionfactory"); Company cp = new Company(); cp.setCompany_name("公司name"); cp.setCompany_description("描述"); Session session = sessionfactory.openSession(); System.out.println("hibernate:" + session.getFlushMode()); UserInfo ui = new UserInfo(); ui.setCompany_name("dsf"); session.save(ui); session.flush(); ui.setCompany_name("company_name_test...."); session.save(ui); ui.setDepartment_description("描述..."); session.flush(); session.save(ui); session.flush(); BaseDao bd = new BaseDao(); bd.setSessionFactory(sessionfactory); System.out.println(bd.getHibernateTemplate().getFlushMode()); System.out.println(bd.getHibernateTemplate().getSessionFactory() .openSession().getFlushMode()); bd.getHibernateTemplate().save(cp); cp.setCompany_name("test————company--"); bd.getHibernateTemplate().save(cp); }
结果:
hibernate:AUTO
Hibernate: insert into t_userinfo (t_username, t_password, t_department_uuid, t_department_tier1level, t_department_tier2level, t_department_tier3level, t_department_description, t_company_id, t_company_name, t_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update t_userinfo set t_username=?, t_password=?, t_department_uuid=?, t_department_tier1level=?, t_department_tier2level=?, t_department_tier3level=?, t_department_description=?, t_company_id=?, t_company_name=? where t_id=?
1
AUTO
Hibernate: insert into t_company (t_company_name, t_company_description, t_company_uuid) values (?, ?, ?)
Hibernate: insert into t_company (t_company_name, t_company_description, t_company_uuid) values (?, ?, ?)
hibernate 默认的 save 之后 不 flush 不clear 数据 所以是insert 之后 是update数据
hibernateTemplate 默认的是操作之后 flush 数据clear数据 所以一直是insert into 数据