Skip to content
Snippets Groups Projects
Commit f44c3b9f authored by Martin Ledvinka's avatar Martin Ledvinka
Browse files

Moved transactional annotations to services + made tests transactional.

parent c33f9638
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,6 @@ import cz.cvut.kbss.ear.setup.model.Teacher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
......@@ -20,12 +19,10 @@ public class TeacherDao {
@PersistenceContext
private EntityManager em;
@Transactional(readOnly = true)
public List<Teacher> findAll() {
return em.createNamedQuery("Teacher.findAll", Teacher.class).getResultList();
}
@Transactional(readOnly = true)
public Teacher findByName(String firstName, String lastName) {
Objects.requireNonNull(firstName);
Objects.requireNonNull(lastName);
......@@ -37,7 +34,6 @@ public class TeacherDao {
}
}
@Transactional
public void persist(Teacher teacher) {
Objects.requireNonNull(teacher);
em.persist(teacher);
......
......@@ -4,6 +4,7 @@ import cz.cvut.kbss.ear.setup.dao.TeacherDao;
import cz.cvut.kbss.ear.setup.model.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
......@@ -13,14 +14,17 @@ public class TeacherService {
@Autowired
private TeacherDao teacherDao;
@Transactional(readOnly = true)
public List<Teacher> findAll() {
return teacherDao.findAll();
}
@Transactional(readOnly = true)
public boolean exists(String firstName, String lastName) {
return teacherDao.findByName(firstName, lastName) != null;
}
@Transactional
public void persist(Teacher teacher) {
teacherDao.persist(teacher);
}
......
......@@ -19,10 +19,11 @@ import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* This class configures our tests so that we can use the Spring features in them - e.g. autowiring.
*
* <p>
* It is often good to extract this setup into a common superclass, so that we need not set the configuration on every test class.
*/
@RunWith(SpringJUnit4ClassRunner.class) // Tell JUnit to use Spring's test runner
......@@ -33,5 +34,7 @@ won't be available for autowiring in tests inheriting from this class.
@ContextConfiguration(classes = {PersistenceConfig.class})
// Reset the Spring context after each tests, recreating all the beans
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
//extend the transactions to whole tests in order to rollback the changes after each test
@Transactional(transactionManager = "txManager")
public abstract class BaseDaoTestRunner {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment