I've encountered a problem I don't really understand - unless a method working with the DAO is annotated as @Transactional, the underlying database doesn't get updated. My app runs on JPA/Hibernate, Spring and Wicket. Why is that?
DAO:
@Repository(value = "userDao")
public class UserDaoJpa implements UserDao {
@PersistenceContext
private EntityManager em;
public User findById(Long id) {
return em.find(User.class, id);
}
public List findAll() {
Query query = em.createQuery("select e from User e");
return query.getResultList();
}
public User create(User user) {
em.persist(user);
return user;
}
public User update(User user) {
return em.merge(user);
}
public void delete(User user) {
user = em.merge(user);
em.remove(user);
}
}
Service:
@Service(value = "userManager")
public class UserManagerImpl implements UserManager {
@Autowired
UserDao dao;
public void setUserDao(UserDao dao) {
this.dao = dao;
}
public List getUsers() {
return dao.findAll();
}
public User getUser(String userId) {
return dao.findById(Long.valueOf(userId));
}
public void saveUser(User user) {
dao.update(user);
}
@Transactional
public void removeUser(User user) {
dao.delete(user);
}
}
In case I leave out the @Transactional annotation, the database doesn't get updated.