Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:49:30+00:00 2026-06-15T17:49:30+00:00

I develop application using Spring, Hibernate and MySQL. I created DAO for one of

  • 0

I develop application using Spring, Hibernate and MySQL. I created DAO for one of my tables. Now, I would like to test DAO’s methods (save, find etc.). The problem is that these methods affect on database data, so I would like to rollback all changes after particular method execution. I tried do this by setting defaultRollback=true in @TransactionConfiguration but it doesn’t work. Below I paste most important fragments of code. Does anyone know how to force rollback after each method?

My table in MySQL is using InnoDB engine.
In fact, after test execution console contains this information:
INFO: Rolled back transaction after test execution for test context ... but changes in database are commited.

UsersDAOTest

@ContextConfiguration(locations={"classpath:applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class UsersDAOTest {

    @Autowired
    UsersHibernateDAO usersDAO;

    @Test
    public void test1() {
        List<Users> results = usersDAO.findAll();
        Assert.assertEquals(0, results.size());
    }

    @Test
    public void test2() {
        Users user = new Users("mchrobok", "12345678901234567890123456789012");
        usersDAO.saveOrUpdate(user);
        List<Users> results = usersDAO.findAll();
        Assert.assertEquals(1, results.size());     
    }   
}    

Hibernate (hibernate.cfg.xml)

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>    
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>

        <mapping class="pl.fp.microblog.domain.Users"/>
    </session-factory>
</hibernate-configuration>

Spring configuration (applicationContext.xml)

<beans>
     <context:component-scan base-package="pl.fp.microblog" />
     <tx:annotation-driven />

     <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"    destroy-method="close">
         <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
         <beans:property name="url" value="jdbc:mysql://localhost:3306/Microblog"/>
         <beans:property name="username" value="root"/>
         <beans:property name="password" value="root"/>
     </beans:bean>

     <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
     <beans:property name="dataSource" ref="dataSource" />
         <beans:property name="configLocation">
             <beans:value>
                 hibernate.cfg.xml
         </beans:value>
         </beans:property>
     </beans:bean>

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

     <beans:bean name="usersDAO" class="pl.fp.microblog.dao.UsersHibernateDAO">
         <beans:property name="sessionFactory" ref="sessionFactory" />
     </beans:bean>
</beans:beans>

EDIT

UsersDAO (it’s generic, but it doesn’t matter)

public class GenericHibernateDAO<T> implements GenericDAO<T> {
    private SessionFactory sessionFactory;
    private Class<?> persistClass;

    public GenericHibernateDAO() {
        ParameterizedType type = ((ParameterizedType)getClass().getGenericSuperclass());
        persistClass = ((Class<?>) type.getActualTypeArguments()[0]);
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public List<T> findAll() {
        Criteria criteria = sessionFactory.openSession().createCriteria(persistClass);
        return criteria.list();
    }

    @Override
    public void saveOrUpdate(T entity) {
        sessionFactory.openSession().saveOrUpdate(entity);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:49:31+00:00Added an answer on June 15, 2026 at 5:49 pm

    You are opening a new session in your hibernate code, you should use getCurrentSession() instead.

    The problem in your code is that your DAO class tries to manage transactions on its own. In modern Spring applications the transaction management should be performed in the service layer.

    So it means that typically your service methods should be annotated with @Transactional.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to develop a web application in Java/Spring/Hibernate serving as a business
I am trying to develop an application using Spring 2.5.5 Flex/J2EE and Hibernate 3.1.3.
I develop an application using Struts, Spring, and Hibernate. My DAOs uses spring jdbc
I'm using Spring (Roo specifically) to develop an application and for one entity I
I am going to develop a Desktop application using Swing and Spring, Hibernate. My
I have a one red5 application using flex. Now I just want to develop
I'm starting to develop an application using MySQL and although I've developed apps before
I am trying to develop an application using ADO.NET. I have two tables in
I want to develop mysql database application using dbexpress to develop from scratch or
i have a general question : i'd like to develop a facebook application using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.