I am a beginner in Spring Data JDBC. I am following Spring in action 6th edition and have created the repository as follows:
package com.springinaction.tacocloud;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
public interface IngredientRepository extends CrudRepository<Ingredient, String>{
public Optional<Ingredient> getById(String id);
public Iterable<Ingredient> getIngredients();
public Ingredient save(Ingredient ingredient);
}
And I have injected this repository in my controller.
@Slf4j
@Controller
@RequestMapping("/design")
public class DesignTacoController {
protected IngredientRepository ingredientRepo;
public DesignTacoController(IngredientRepository ingredientRepo) {
this.ingredientRepo = ingredientRepo;
}
@ModelAttribute
public void addIngredientsToModel(Model model) {
Iterable<Ingredient> ingredientsFromJdbc = ingredientRepo.getIngredients();
//converting Iterable returned by JdbcTemplate to a List
List<Ingredient> ingredients = new ArrayList<Ingredient>();
ingredientsFromJdbc.forEach(ingredients::add);
Type[] types = Ingredient.Type.values();
for (Type type : types) {
model.addAttribute(type.toString().toLowerCase(),filterByType(ingredients, type));
}
}
.......................................................................
For invoking the method getIngredients() in the controller it throws the following exception
nested exception is org.springframework.data.mapping.PropertyReferenceException: No property 'getIngredients' found for type 'Ingredient'!
What am I missing here?
CodePudding user response:
Just remove the method getIngredients()
and use findAll()
(provided by the super interface) instead.
You should also remove the other methods, because they are already provided by the super interface
save()
<- provided byCrudRepository
getById()
<-CrudRepository
provideOptional<T> findById(ID id);
getIngredients
<-CrudRepository
provideIterable<T> findAll();