I have added @ComponentScan, @EntityScan in the main class still the same error, I'm new to this and getting this error from few days. Please help to solve this.
**StackTrace: **
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()!
Main Class
@SpringBootApplication
//@ComponentScan(basePackages = {"org.simplilearn"})
//@EntityScan(basePackages = "org.simplilearn")
public class MedicareApplication {
public static void main(String[] args) {
SpringApplication.run(MedicareApplication.class, args);
}
}
UserService
@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();
}
}
UserRepository
@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();
}
User Entity
@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 + "]";
}
}
I have added @ComponentScan, @EntityScan in the main class still the same error, I'm new to this and getting this error from few days. Please help to solve this.
@SpringBootApplication has already @ComponentScan in it. You ca hover on the annotation and see the list. It scans the folder com.example.myApplication folder under Java. If your components are not here then it can not scan.
I would also check the column names in query if you have any typos.