gpt4 book ai didi

java - 为什么 FetchType.EAGER 在单元测试中不起作用?

转载 作者:行者123 更新时间:2023-12-02 10:15:06 26 4
gpt4 key购买 nike

我的应用程序使用 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();
}
}

当我在主类中运行代码时,它会加入实体:

主要

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 中它正在工作,因为您有两个事务,并且在读取中是在一个单独的事务中,并且 Department 是从数据库中获取的。

但在单元测试中,它从用户列表为空的内存中返回部门。

在保存实体之前保持正确的关系设置非常重要。

关于java - 为什么 FetchType.EAGER 在单元测试中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54741038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com