当前位置: 首页 > 知识库问答 >
问题:

创建bean userController时出错,原因是无法为公共抽象java创建查询。util。可选择的

燕璞
2023-03-14

堆栈跟踪:组织。springframework。豆。工厂UnsatisfiedDependencyException:创建名为“userController”的bean时出错:通过字段“userRepository”表示的未满足的依赖关系;嵌套异常为org。springframework。豆。工厂BeanCreationException:创建br中定义的名为“userRepository”的bean时出错。通用域名格式。艾伦。flashfood。领域存储库。在JpaRepositoriesRegistrar上声明的@EnableJpaRepositories中定义的用户存储库。EnableJpaRepositoriesConfiguration:初始化方法调用失败;嵌套异常为org。springframework。数据存储库。查询QueryCreationException:无法为公共抽象java创建查询。util。可选br。通用域名格式。艾伦。flashfood。领域存储库。CustomJpaRepository。findFirst()!原因:无法为方法公共抽象java创建查询。util。可选br。通用域名格式。艾伦。flashfood。领域存储库。CustomJpaRepository。findFirst()!找不到类型User!的属性findFirst!;嵌套的异常是java。lang.IllegalArgumentException:无法为方法公共抽象java创建查询。util。可选br。通用域名格式。艾伦。flashfood。领域存储库。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);
    }
}

共有1个答案

谈渊
2023-03-14

方法findFirst()本身不存在。您需要添加更多表达式,例如findFirstByName(String name)。换句话说,您将无法使用可以正确继承的findFirst方法创建超类。我在这里并没有真正看到我可以创建另一个接口来扩展的情况,您可能最好从您的UserRepository扩展JpaRepository

 类似资料: