为什么 FetchType.EAGER 在单元测试中不起作用?
Why FetchType.EAGER doesn't work in unit tests?
我的 JPA (Hibernate) 应用程序和 Spring。我的实体是:
部门
@Entity
@Table(schema = "myschema")
public class Department {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)", length = 16 )
private UUID uuid;
@Column(name = "name", length = 200, nullable = false)
private String name;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)
List<User> users = new ArrayList<>();
}
用户
@Entity
@Table(schema = "myschema")
public class User {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)", length = 16 )
private UUID uuid;
@NotNull
@Column(name = "login", length = 100, nullable = false)
private String login;
@ManyToOne
@JoinColumn(name = "department_fk", nullable = false)
private Department department;
}
DAO 部门:
@Repository("departmentDao")
public class DepartmentDao {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public UUID save(Department entity) {
log.info("Department entity will be persisted", entity);
entityManager.persist(entity);
return entity.getUuid();
}
@Transactional
public List<Department> getAllWithUsers() {
log.info("Select all departments");
CriteriaQuery<Department> criteria = entityManager.getCriteriaBuilder().createQuery(Department.class);
Root<Department> root = criteria.from(Department.class);
criteria.select(root);
return entityManager.createQuery(criteria).getResultList();
}
}
当我 运行 Main class 中的代码加入实体时:
主要
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring.xml");
UserDao userDao = (UserDao) context.getBean("userDao");
DepartmentDao departmentDao = (DepartmentDao) context.getBean("departmentDao");
Department newDep = new Department("WebDepartment", new Date());
departmentDao.save(newDep);
User newUser1 = new User("Test1", newDep);
User newUser2 = new User("Test2", newDep);
userDao.save(newUser1);
userDao.save(newUser2);
List<Department> deps = departmentDao.getAllWithUsers();
//deps
//[Department{name='WebDepartment', users = ['Test1', 'Test2']}]
当我 运行 使用相同代码进行单元测试时,该部门的用户列表为空:
@ContextConfiguration(locations = "classpath:META-INF/spring-test.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class DaoTests {
@Autowired
private DepartmentDao departmentDao;
@Autowired
private UserDao userDao;
@Test
@Transactional
public void daoTests() throws NoSuchAlgorithmException {
Department newDep = new Department("WebDepartment", new Date());
departmentDao.save(newDep);
User newUser1 = new User("Test1", newDep);
User newUser2 = new User("Test2", newDep);
userDao.save(newUser1);
userDao.save(newUser2);
List<Department> deps = departmentDao.getAllWithUsers();
//deps
//[Department{name='WebDepartment', users = []}]
}
}
所以,我的问题是:为什么 EAGER 在 Main 中有效,但在测试中却无效?
我假设您没有将新用户添加到部门的用户列表中。
您的构造函数必须如下所示:
public User(
// other fields,
Department department) {
// set all fields.
department.getUsers().add(this);
}
在 Main 中,它可以正常工作,因为您有两个事务,并且在读取中是在一个单独的事务中,并且部门是从 DB 中获取的。
但是在单元测试中 returns 用户列表所在的内存部门是空的。
在保存实体之前正确设置关系非常重要。
我的 JPA (Hibernate) 应用程序和 Spring。我的实体是:
部门
@Entity
@Table(schema = "myschema")
public class Department {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)", length = 16 )
private UUID uuid;
@Column(name = "name", length = 200, nullable = false)
private String name;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)
List<User> users = new ArrayList<>();
}
用户
@Entity
@Table(schema = "myschema")
public class User {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)", length = 16 )
private UUID uuid;
@NotNull
@Column(name = "login", length = 100, nullable = false)
private String login;
@ManyToOne
@JoinColumn(name = "department_fk", nullable = false)
private Department department;
}
DAO 部门:
@Repository("departmentDao")
public class DepartmentDao {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public UUID save(Department entity) {
log.info("Department entity will be persisted", entity);
entityManager.persist(entity);
return entity.getUuid();
}
@Transactional
public List<Department> getAllWithUsers() {
log.info("Select all departments");
CriteriaQuery<Department> criteria = entityManager.getCriteriaBuilder().createQuery(Department.class);
Root<Department> root = criteria.from(Department.class);
criteria.select(root);
return entityManager.createQuery(criteria).getResultList();
}
}
当我 运行 Main class 中的代码加入实体时:
主要
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring.xml");
UserDao userDao = (UserDao) context.getBean("userDao");
DepartmentDao departmentDao = (DepartmentDao) context.getBean("departmentDao");
Department newDep = new Department("WebDepartment", new Date());
departmentDao.save(newDep);
User newUser1 = new User("Test1", newDep);
User newUser2 = new User("Test2", newDep);
userDao.save(newUser1);
userDao.save(newUser2);
List<Department> deps = departmentDao.getAllWithUsers();
//deps
//[Department{name='WebDepartment', users = ['Test1', 'Test2']}]
当我 运行 使用相同代码进行单元测试时,该部门的用户列表为空:
@ContextConfiguration(locations = "classpath:META-INF/spring-test.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class DaoTests {
@Autowired
private DepartmentDao departmentDao;
@Autowired
private UserDao userDao;
@Test
@Transactional
public void daoTests() throws NoSuchAlgorithmException {
Department newDep = new Department("WebDepartment", new Date());
departmentDao.save(newDep);
User newUser1 = new User("Test1", newDep);
User newUser2 = new User("Test2", newDep);
userDao.save(newUser1);
userDao.save(newUser2);
List<Department> deps = departmentDao.getAllWithUsers();
//deps
//[Department{name='WebDepartment', users = []}]
}
}
所以,我的问题是:为什么 EAGER 在 Main 中有效,但在测试中却无效?
我假设您没有将新用户添加到部门的用户列表中。
您的构造函数必须如下所示:
public User(
// other fields,
Department department) {
// set all fields.
department.getUsers().add(this);
}
在 Main 中,它可以正常工作,因为您有两个事务,并且在读取中是在一个单独的事务中,并且部门是从 DB 中获取的。
但是在单元测试中 returns 用户列表所在的内存部门是空的。
在保存实体之前正确设置关系非常重要。