@Testpublic void test () {ApplicationContext ac = new Classpathxmlapplicationcontext ("Spring_dao.xml"); sessionfactory sessionfactory = (sessionfactory) ac.getbean ("SessionFactory"); Company cp = new company (); Cp.setcompany_name ("Company name"), Cp.setcompany_description ("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 ("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);
Results:
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 default save after not flush not clear data so is insert after IS update data
Hibernatetemplate default is flush data clear data after operation so always insert into data
Hibernatetemplate and Hibernate session save data is not the same