Learner
Learner

Reputation: 49

Getting a list of results by using CRUD Repository

I am new to using CRUD Repository.

I have a database table with three columns:

course_id, name, course

I want to get a list of course_id give name, example,

SELECT id FROM table WHERE name='charmaine'; 

However, I do not want to do it with query but using crud repository.

There is an error shown in my controller. May I know there is this error?

My controller

@GetMapping(value = "getting/{name}")
    //@ResponseBody
     public String getting(@PathVariable("name") String name) {
        List<CourseMaster> getIds =  CourseService.findIdByName(Name);   —> error icon here
        return getIds;  —> error icon here
     }

Service

public List<CourseMaster> findIdByName(String Name) {
    return CourseMasterRepo.findByName(Name);
}

Repository

public interface CourseMasterRepo extends CrudRepository<CourseMaster, Long> {
    List<CourseMaster> findByName(String Name);
}

Upvotes: 0

Views: 1117

Answers (2)

Nikunj M
Nikunj M

Reputation: 31

You have to autowired service class in your controller like.

 @Autowired
 CourseService courseService;

 @GetMapping(value = "getting/{name}")
 public String getting(@PathVariable("name") String name) {
    List<CourseMaster> getIds =  courseService.findIdByName(Name);
    return getIds;
 }

Upvotes: 1

hamid_reza hobab
hamid_reza hobab

Reputation: 929

if your code is done in java spring you must use hql language in hibernate, that is a interface of sql query.

hql query that use lambda expression is very simple and useful.

For example,

String hql = "FROM Employee E WHERE E.id = 10"; 
Query query = session.createQuery(hql); 
List results = query.list();

Upvotes: 0

Related Questions