Could not create query for public abstract java.util.optional 导致创建 bean userController 时出错
Error in creating bean userController caused by Could not create query for public abstract java.util.optional
堆栈跟踪:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'userController' 的 bean 时出错:
通过字段 'userRepository' 表示的未满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:
在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上声明的 @EnableJpaRepositories 中定义的 br.com.allen.flashfood.domain.repository.UserRepository 中定义的名称 'userRepository' 创建 bean 时出错:调用 init 方法失败;
嵌套异常是 org.springframework.data.repository.query.QueryCreationException:
无法为 public 摘要 java.util.Optional br.com.allen.flashfood.domain.repository.CustomJpaRepository.findFirst() 创建查询!
原因:无法为方法 public 抽象 java.util.Optional br.com.allen.flashfood.domain.repository.CustomJpaRepository.findFirst() 创建查询!
未找到 属性 类型为 User 的 findFirst!;嵌套异常是 java.lang.IllegalArgumentException:无法为方法 public 抽象 java.util.Optional 创建查询
br.com.allen.flashfood.domain.repository.CustomJpaRepository.findFirst()!
未找到 属性 类型为 User 的 findFirst!
类:
- 控制器:
package br.com.allen.flashfood.api.controller;
import br.com.allen.flashfood.api.assembler.UserModelAssembler;
import br.com.allen.flashfood.api.assembler.UserRequestDisassembler;
import br.com.allen.flashfood.api.model.request.PasswordRequest;
import br.com.allen.flashfood.api.model.request.UserPasswordRequest;
import br.com.allen.flashfood.api.model.request.UserRequest;
import br.com.allen.flashfood.api.model.response.UserResponse;
import br.com.allen.flashfood.domain.model.User;
import br.com.allen.flashfood.domain.repository.UserRepository;
import br.com.allen.flashfood.domain.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
private UserService userService;
@Autowired
private UserModelAssembler userModelAssembler;
@Autowired
private UserRequestDisassembler userRequestDisassembler;
@GetMapping
public List<UserResponse> getAllUsers() {
List<User> allUsers = userRepository.findAll();
return userModelAssembler.toCollectionModel(allUsers);
}
@GetMapping("/{userId}")
public UserResponse getUserById(@PathVariable Long userId) {
User user = userService.findUserOrElseThrow(userId);
return userModelAssembler.toModel(user);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public UserResponse addUser(@RequestBody @Valid UserPasswordRequest userPasswordRequest) {
User user = userRequestDisassembler.toDomainObject(userPasswordRequest);
user = userService.saveUser(user);
return userModelAssembler.toModel(user);
}
@PutMapping("/{userId}")
public UserResponse updateUser(@PathVariable Long userId,
@RequestBody @Valid UserRequest userRequest) {
User actualUser = userService.findUserOrElseThrow(userId);
userRequestDisassembler.copyToDomainObject(userRequest, actualUser);
actualUser = userService.saveUser(actualUser);
return userModelAssembler.toModel(actualUser);
}
@PutMapping("/{userId}/password")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void userPassword(@PathVariable Long userId,
@RequestBody @Valid PasswordRequest passwordRequest) {
userService.changePassword(userId, passwordRequest.getActualPassword(), passwordRequest.getNewPassword());
}
}
存储库:
package br.com.allen.flashfood.domain.repository;
import br.com.allen.flashfood.domain.model.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CustomJpaRepository<User, Long> {
}
CustomJpaRepository
package br.com.allen.flashfood.domain.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import java.util.Optional;
@NoRepositoryBean
public interface CustomJpaRepository<T, ID> extends JpaRepository<T, ID> {
Optional<T> findFirst();
}
用户:
package br.com.allen.flashfood.domain.model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
@Entity
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@EqualsAndHashCode.Include
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String password;
@CreationTimestamp
@Column(nullable = false, columnDefinition = "datetime")
private OffsetDateTime registrationDate;
@ManyToMany
@JoinTable(name = "user_group",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "group_id"))
private List<Family> groups = new ArrayList<>();
public boolean passwordConfirmed(String password) {
return getPassword().equals(password);
}
public boolean passwordNotConfirmed(String password) {
return !passwordConfirmed(password);
}
}
编辑 1:
我忘了对实施提出质疑,因为我认为现在编辑问题不可能是这样。
package br.com.allen.flashfood.infrastructure.repository;
import br.com.allen.flashfood.domain.repository.CustomJpaRepository;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import javax.persistence.EntityManager;
import java.util.Optional;
public class CustomJpaRepositoryImpl<T, ID> extends SimpleJpaRepository<T, ID> implements CustomJpaRepository<T, ID> {
private EntityManager entityManager;
public CustomJpaRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
@Override
public Optional<T> findFirst() {
var jpql = "from " + getDomainClass().getName();
T entity = entityManager.createQuery(jpql, getDomainClass())
.setMaxResults(1)
.getSingleResult();
return Optional.ofNullable(entity);
}
}
方法findFirst()
本身不存在。您需要为此添加更多表达式,例如 findFirstByName(String name)
。换句话说,您将无法使用可以正确继承的 findFirst
方法创建超类。我真的没有在这里看到我会创建另一个接口来扩展的情况,你可能最好从你的 UserRepository
.
扩展 JpaRepository
堆栈跟踪: org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'userController' 的 bean 时出错: 通过字段 'userRepository' 表示的未满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException: 在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上声明的 @EnableJpaRepositories 中定义的 br.com.allen.flashfood.domain.repository.UserRepository 中定义的名称 'userRepository' 创建 bean 时出错:调用 init 方法失败; 嵌套异常是 org.springframework.data.repository.query.QueryCreationException: 无法为 public 摘要 java.util.Optional br.com.allen.flashfood.domain.repository.CustomJpaRepository.findFirst() 创建查询! 原因:无法为方法 public 抽象 java.util.Optional br.com.allen.flashfood.domain.repository.CustomJpaRepository.findFirst() 创建查询! 未找到 属性 类型为 User 的 findFirst!;嵌套异常是 java.lang.IllegalArgumentException:无法为方法 public 抽象 java.util.Optional 创建查询 br.com.allen.flashfood.domain.repository.CustomJpaRepository.findFirst()! 未找到 属性 类型为 User 的 findFirst!
类:
- 控制器:
package br.com.allen.flashfood.api.controller;
import br.com.allen.flashfood.api.assembler.UserModelAssembler;
import br.com.allen.flashfood.api.assembler.UserRequestDisassembler;
import br.com.allen.flashfood.api.model.request.PasswordRequest;
import br.com.allen.flashfood.api.model.request.UserPasswordRequest;
import br.com.allen.flashfood.api.model.request.UserRequest;
import br.com.allen.flashfood.api.model.response.UserResponse;
import br.com.allen.flashfood.domain.model.User;
import br.com.allen.flashfood.domain.repository.UserRepository;
import br.com.allen.flashfood.domain.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
private UserService userService;
@Autowired
private UserModelAssembler userModelAssembler;
@Autowired
private UserRequestDisassembler userRequestDisassembler;
@GetMapping
public List<UserResponse> getAllUsers() {
List<User> allUsers = userRepository.findAll();
return userModelAssembler.toCollectionModel(allUsers);
}
@GetMapping("/{userId}")
public UserResponse getUserById(@PathVariable Long userId) {
User user = userService.findUserOrElseThrow(userId);
return userModelAssembler.toModel(user);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public UserResponse addUser(@RequestBody @Valid UserPasswordRequest userPasswordRequest) {
User user = userRequestDisassembler.toDomainObject(userPasswordRequest);
user = userService.saveUser(user);
return userModelAssembler.toModel(user);
}
@PutMapping("/{userId}")
public UserResponse updateUser(@PathVariable Long userId,
@RequestBody @Valid UserRequest userRequest) {
User actualUser = userService.findUserOrElseThrow(userId);
userRequestDisassembler.copyToDomainObject(userRequest, actualUser);
actualUser = userService.saveUser(actualUser);
return userModelAssembler.toModel(actualUser);
}
@PutMapping("/{userId}/password")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void userPassword(@PathVariable Long userId,
@RequestBody @Valid PasswordRequest passwordRequest) {
userService.changePassword(userId, passwordRequest.getActualPassword(), passwordRequest.getNewPassword());
}
}
存储库:
package br.com.allen.flashfood.domain.repository;
import br.com.allen.flashfood.domain.model.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CustomJpaRepository<User, Long> {
}
CustomJpaRepository
package br.com.allen.flashfood.domain.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import java.util.Optional;
@NoRepositoryBean
public interface CustomJpaRepository<T, ID> extends JpaRepository<T, ID> {
Optional<T> findFirst();
}
用户:
package br.com.allen.flashfood.domain.model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
@Entity
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@EqualsAndHashCode.Include
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String password;
@CreationTimestamp
@Column(nullable = false, columnDefinition = "datetime")
private OffsetDateTime registrationDate;
@ManyToMany
@JoinTable(name = "user_group",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "group_id"))
private List<Family> groups = new ArrayList<>();
public boolean passwordConfirmed(String password) {
return getPassword().equals(password);
}
public boolean passwordNotConfirmed(String password) {
return !passwordConfirmed(password);
}
}
编辑 1: 我忘了对实施提出质疑,因为我认为现在编辑问题不可能是这样。
package br.com.allen.flashfood.infrastructure.repository;
import br.com.allen.flashfood.domain.repository.CustomJpaRepository;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import javax.persistence.EntityManager;
import java.util.Optional;
public class CustomJpaRepositoryImpl<T, ID> extends SimpleJpaRepository<T, ID> implements CustomJpaRepository<T, ID> {
private EntityManager entityManager;
public CustomJpaRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
@Override
public Optional<T> findFirst() {
var jpql = "from " + getDomainClass().getName();
T entity = entityManager.createQuery(jpql, getDomainClass())
.setMaxResults(1)
.getSingleResult();
return Optional.ofNullable(entity);
}
}
方法findFirst()
本身不存在。您需要为此添加更多表达式,例如 findFirstByName(String name)
。换句话说,您将无法使用可以正确继承的 findFirst
方法创建超类。我真的没有在这里看到我会创建另一个接口来扩展的情况,你可能最好从你的 UserRepository
.
JpaRepository