Frage

I'm currently on learning integration of Spring + Hibernate, and so far I'm getting it to work. But I've run into situation where I dont want to commit the transaction at the end of process because I just want to see the generated sql stattement for testing and debugging purposes.

I have already added false to my hibernate properties, but then It still doesn't working.

Is it possible to achieve if using Spring to handle hibernate transaction? because in using traditional transaction in hibernate , it is possible, just don't call the session.commit() method and all the updates and insert will not be saved;

currently I have this code for my service layer :

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) public class EmployeeServiceImpl{

@Autowired
private EmployeeDao employeeDao

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void saveEmployee(Employee employee){

    // count the number of employee in the database
    int n  = employeeDao.getEmployeeCount();
    // lets say it should print 10 here
    System.out.println("number of employee before inserting : " + n);

    // insert to employee
    employeeDao.saveEmployee(employee);

    // then count the employee after inserting
    n  =  employeeDao.getEmployeeCount();

    // then it should print 11 here after inserting new employee
    System.out.println("number of employee after inserting : " + n);

/* Then Dont commit insert!!!
*/

}

}

Code for Dao layer :

public EmployeeDaoImpl implements EmployeeDao{

@Autowired
public void saveEmployee(Employee employee){

    sessionFactory.getCurrentSession.save(employee);

}

public int getEmployeeCount(){

    String count = sessionFactory.getCurrentSesion.createQuery("select count(emp.employeeId) from Employee emp")
    .list().get(0).toString;

return Integer.valueOf(count);
}

}

But what happens here is that it does commit the transaction!, But I dont want to commit the transaction just for testing and debugging purposes.

I've also tried @Transactional(propagation = Propagation.SUPPORTS, readOnly = false) instead of @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)

But what happens here is that it does not commit the transaction, however the count of employee does not also incrementing.

So, what I am expecting to happen here is that count the number of employee before inserting to employee table. Then insert to employee table and count again the number of employee, so it would increment by 1. But at the end of the process , I don't want to commit that insert!

Do you have any Idea guys?

I would be very thankful for any help! Thanks!

War es hilfreich?

Lösung

For testing, use the Spring test framework. It handles automatic transaction rollbacks for you. If you're looking for something else, you're going to have to be more specific. Your question, while full of detail, was rather vague about what you actually want to do. You just use words like "test" and "debug" a lot. Those words can mean lots of different things to different people.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top