This works because you marked the transaction as read only with @Transactional(readOnly = true) .
As you can see, this does not make your transactions virtually read-only, as you can still save changes by calling flush() manually. However, it disables automatic reset at the end of the transaction, so changes are not saved without a manual reset.
You need to either remove readOnly from the class level annotation, or override it for non-read methods with method level annotations:
@Override @Transactional(readOnly = false) public boolean save(User user) { ... }
Also note that transaction demarcation is usually applied to service level methods, not DAO methods. In particular, when writing DAO methods, you actually do not know which transactions should be read-only and which should not. This information is only available when developing a service level, as you can see in this example:
public class UserService { @Autowired UserDAO dao; @Transactional(readOnly = true) public User getUserById(int id) { return dao.getById(id);
axtavt
source share