【发布时间】:2021-05-31 10:14:06
【问题描述】:
我有一个driving_info 的实体,其中包含很多字段,但其中一个是电话号码(从中订购)。
我要做的是获取从该号码订购的所有驱动器。但是当我尝试传递 phoneNumber 的 int 时,我得到了
query did not return a unique result: 5; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 5
org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 5; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 5
我实际上想要结果列表,以便我可以获得从该电话号码订购的所有驱动器列表的响应。
我的控制器方法是
@GetMapping("/users/{phone}")
public List<User> getUserByPhone(@PathVariable int phone) {
List<User> users= userService.findByPhone(phone);
if(users == null) {
throw new RuntimeException("User not found with "+phone+" phone number");
}
return users;
}
我的 DAO 是
@Override
@Transactional
public List<User> findByPhone(int phone) {
Session currentSession = entityManager.unwrap(Session.class);
Query<User> theQuery = currentSession.createQuery("from User where phone=:phone",User.class);
List<User> users = theQuery.getResultList();
return users;
}
【问题讨论】: