org.springframework.beans.factory.BeanCreationException:在电子商务项目中

时间:2017-11-03 04:14:38

标签: java spring spring-mvc spring-boot spring-data-jpa

我使用Spring启动项目,当我运行时,我收到以下错误,

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userSecurityService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract Ecommerce.entities.User Ecommerce.repository.UserRepository.findByUsername(java.lang.String)!

我假设最后一行是错误堆栈中的重要内容,

Could not create query metamodel for method public abstract Ecommerce.entities.User Ecommerce.repository.UserRepository.findByUsername(java.lang.String)!

我有下面提供的用户存储库,

public interface UserRepository extends CrudRepository<User, Long> {

    User findByUsername(String username);

    User findByEmail(String email);
}

还提供了用户实体

@Entity
public class User implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "userId", nullable = false, updatable = false)
    private Long userId;

    private String userName;

    private String password;

    private String firstName;

    private String lastName;

    @Column(name = "email", nullable = false, updatable = false)
    private String email;

    private String phone;

    private boolean enabled = true;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
    private ShoppingCart shoppingCart;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<UserShipping> userShippingList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<UserPayment> userPaymentList;

    @OneToMany(mappedBy = "user")
    private List<Order> orderList;

    @JsonIgnore
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<UserRole> userRoles = new HashSet<>(); 


    // .............. 
    // more lines of code for overriding the methods 

}

这里有什么问题以及如何解决?

2 个答案:

答案 0 :(得分:1)

在Spring JPA Repository中,自动生成的查找程序遵循以下命名约定。

findBy<DataMember><Op> 

<Op>可以像,等等。

答案 1 :(得分:0)

显然,方法findByUsername的名称应与用户实体中的属性匹配。在用户实体类中,我使用了

@Entity
public class User implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "userId", nullable = false, updatable = false)
    private Long userId;

    private String userName;
}

我从username更改为userName后,问题就解决了。我在咨询了其他SOF帖后做了这个,但是,我仍然寻求更好的解释。