我在主类中添加了@ComponentScan,@EntityScan仍然是相同的错误,我是新手,几天来就收到了这个错误。请帮忙解决这个问题。
**堆栈跟踪:**
Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepo' : nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in org.simplilearn.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List org.simplilearn.repository.UserRepository.joinUserProductTable(); Reason: Validation failed for query for method public abstract java.util.List org.simplilearn.repository.UserRepository.joinUserProductTable()!; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List org.simplilearn.repository.UserRepository.joinUserProductTable()!
主课
@SpringBootApplication
//@ComponentScan(basePackages = {"org.simplilearn"})
//@EntityScan(basePackages = "org.simplilearn")
public class MedicareApplication {
public static void main(String[] args) {
SpringApplication.run(MedicareApplication.class, args);
}
}
用户服务
@Service
public class UserService {
@Autowired
private UserRepository userRepo;
// Save the User to the database(POST METHOD)
public User saveUser(User user)
{
return userRepo.save(user);
}
//Fetch the User from Database(GET METHOD)
public List<User> getUsers(){
return userRepo.findAll();
}
public User getUserById(int id) {
return userRepo.findById(id).orElse(null);
}
//Delete the User from Database (DELETE METHOD)
public String deleteUserById(int id) {
userRepo.deleteById(id);
return "Data" +id+ "Deleted Successfully!";
}
//Join the User and Product Table
public List<OrderResponse> getUserProductJoinInfos(){
return userRepo.joinUserProductTable();
}
}
用户存储库
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
@Query("SELECT new org.simplilearn.entity.OrderResponse(u.UserId,u.name,u.email, u.mobile, u.address, p.medicinename, p.seller"+ ", p.price, p.description, p.quantity, p.orderDateTime) FROM User u JOIN u.products p")
public List<OrderResponse> joinUserProductTable();
}
用户实体
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int UserId;
private int age;
private String name;
private String email;
private String gender;
private String address;
private String mobile;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "USER_PRODUCT_TABLE", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "UserId") }, inverseJoinColumns = { @JoinColumn(name = "product_id", referencedColumnName = "pid") })
private List<Product> products;
public User() {
}
public User( int age, String name, String email, String gender, String address, String mobile, List<Product> products) {
super();
this.age = age;
this.name = name;
this.email = email;
this.gender = gender;
this.address = address;
this.mobile = mobile;
this.products = products;
}
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
@Override
public String toString() {
return "User [UserId=" + UserId + ", age=" + age + ", name=" + name + ", email=" + email + ", gender=" + gender
+ ", address=" + address + ", mobile=" + mobile + ", products=" + products + "]";
}
}
我在主类中添加了@ComponentScan,@EntityScan仍然是相同的错误,我是新手,几天来就收到了这个错误。请帮忙解决这个问题。
@SpringBootApplication 已经有 @ComponentScan 了。您可以将鼠标悬停在注释上并查看列表。它扫描 Java 下的文件夹 com.example.myApplication 文件夹。如果您的组件不在这里,则无法扫描。
如果有任何拼写错误,我还会检查查询中的列名称。