Resource r=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(r); HibernateTemplate template = (HibernateTemplate) factory.getBean("template"); HibernateTransactionManager txManager = (HibernateTransactionManager) factory.getBean ("txManager"); TransactionDefinition txDef= new DefaultTransactionDefinition(); TransactionStatus ts= txManager.getTransaction(txDef); Employee acc=template.load(Employee.class,114,LockMode.READ); acc.setName("varun"); template.update(acc); txManager.commit(ts);
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/spring/applicationContext-services-test.xml") @TestExecutionListeners(inheritListeners = false, listeners = { SpecialDependencyInjectionTestExcecutionListener.class }) @TransactionConfiguration(defaultRollback = true) @Transactional public class loadTest { ... private static HibernateTransactionManager transactionManager; ... @BeforeClass public static void setupDB() { //- set the transaction to rollback only. We have to get a new transaction for that. DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); transactionManager.getTransaction(def).setRollbackOnly(); ... //- start loading the data using the injected services. ... }
@Override public final void setUp() throws Exception { super.setUp(); SessionFactory sessionFactory = InstanceFactory .getInstance(SessionFactory.class); tm = InstanceFactory.getInstance(HibernateTransactionManager.class); if (tm != null) { ts = tm.getTransaction(new DefaultTransactionDefinition()); } if (sessionFactory != null) { session = sessionFactory.openSession(); tx = session.beginTransaction(); } action4SetUp(); }