1

This is tge error i am getting atthe time of compilation. I am new at spring and not able to understand the error.Plz help me to find out.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepo' defined in com.example.jpa.repository.UserRepo 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 com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! No property findName found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.jpa.repository.UserRepo.findNameLike(java.lang.String)! No property findName found for type User!

This is my Repository class

According to that the error in the @Query But i am getting how to fix and wher is my mistake. i have created a tavle called user1 in mysql.

package com.example.jpa.repository;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder.In;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.jpa.entity.User;

@Repository
public interface UserRepo extends JpaRepository<User, Integer> {
    public List<User> findByAgeGreaterThanEqual(int age) ;
    
    public List<User> findByAgeIn(int age);
    
    public List<User> findByNameOrderByName(String name);
    @Modifying
    @Query (value="select * from user1",nativeQuery=true)
    public List<User> getAllUser();
}

This is my Entity class

package com.example.jpa.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
//@Table(name = "user")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private String city;
    private String status;
    
    public User() {
        super();
        
    }
    public User(int id, String name, String city, String status) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
        this.status = status;
    }
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}

This is my main application main class

package com.example.jpa;

import java.util.List;
import java.util.Optional;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.example.jpa.entity.User;
import com.example.jpa.repository.UserRepo;


@SpringBootApplication
public class SpringApplicaton1Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringApplicaton1Application.class, args);
        UserRepo repo = context.getBean(UserRepo.class);

        User user = new User();
        user.setId(1);
        user.setCity("Delhi");
        user.setName("Ramesh");
        user.setStatus("Java Developer");

        List<User> users= repo.getAllUser();
        
        users.forEach(n->{
            System.out.println(n);
            
        });
        
    }

}

This is my Application.property file

spring.datasource.name=test
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/practice   
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect

spring.jpa.hibernate.use-new-id-generator-mappings= false

spring.jpa.hibernate.ddl-auto=update
Mr.Roy
  • 35
  • 1
  • 2
  • 8
  • 1
    Your error message does not match your code; you did not define a query method `findNameLike` in this code. Also, (1) `nativeQuery` is an _absolute last_ resort; your query can trivially be written in JPQL, and (2) `findAll` is already implicitly declared. – chrylis -cautiouslyoptimistic- Jul 30 '21 at 20:34
  • Thank u so much i am able to solve the pblm. – Mr.Roy Aug 01 '21 at 15:29

3 Answers3

0

Use 2 annotations like enablejparepositories , entityscan

  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 20 '21 at 08:01
0

i was solving this problem by add these annotation in model class

@Table //is a corresponding table that matches that entity in the database
@Entity // for specifies class is an entity and is mapped to a database table. 
@Data // for getter and seter
public class UserModel {
}
0
@Query (value="select * from User", nativeQuery=true)

Table name should start with capital U, i.e. same as class name.

Abra
  • 19,142
  • 7
  • 29
  • 41