volkanb
volkanb

Reputation: 269

How to search database for string instead id with findById()?

how can I search for a string Pathvariable instead of a Integer by ID? I like to find the names.

The method below works well, but just with IDs as a Integer value.

Thanks :)


@RestController
@RequestMapping(value = "/etbestandservice")
public class ETBestandserviceController {

    @Autowired
    ETBestandserviceRepository etbestandRepos;

        @GetMapping(value = "/find/{id}", produces = "application/json")
        public Optional<ETBestandservice> findErsatzteile(@PathVariable("id") Integer id, ETBestandservice et) {

        id = et.getId();
        return etbestandRepos.findById(id);
    }
}

Upvotes: 0

Views: 1485

Answers (1)

davidxxx
davidxxx

Reputation: 131436

Add an Optional<ETBestandservice> findByName(String name) method in your Spring JPA ETBestandserviceRepository interface and Spring will generate the expected query for you for that at startup.
If the query may return several results, replace Optional<ETBestandservice> by List<ETBestandservice>.
More information here.

Upvotes: 1

Related Questions