2

I have written JPA code in Spring boot where I want to perform CRUD and other operations on an Entity, I have written RecipeRepository that extends JpaRepository

public interface RecipeRepository extends  JpaRepository<Recipe,Long> {

  public List<Recipe> findByName(String name);

  public Recipe findOneByName(String name);
}

and Entity class is;

@Entity
@Table(name = "Recipe")
public class Recipe {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;

@Column(name = "name")
private String name;
..

} 

When I call the the recipeRepository.findByName("test") or recipeRepository.findOneByName("test"), I get null. When I call findAll() and then itreate over the values, I can find Recipe where name is test

String name = "test";

Recipe recipe = recipeRepository.findOneByName(name);

List<Recipe> recipeList = recipeRepository.findByName(name);

Iterable<Recipe> recipies = recipeRepository.findAll();
for(Recipe recipe : recipies){
    System.out.println(recipe.getName());
    // gets value of recipe where name is test
}

in the logs for the findByName or findOneByName, I get the following in logs:

select recipe0_.id as id1_0_, recipe0_.is_active as is_activ2_0_, recipe0_.is_injected as is_injec3_0_, recipe0_.name as name4_0_, recipe0_.rule as rule5_0_ from recipe recipe0_ where recipe0_.name=?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Adnan Mian
  • 415
  • 1
  • 5
  • 17
  • can u post implementation of ur method findByName()? – vihang shah Jul 03 '17 at 17:26
  • 3
    Some questions - In the DB, is there a space before or after test. And is your database case sensitive ? or case insensitive. – Prashant Jul 03 '17 at 17:33
  • Can you pls use findByUsername instead of findByName – Pradeep Jul 03 '17 at 17:54
  • @Prashant thanks for pointing that out, I was passing wrong value of the param to my controller... instead of : **localhost:8080/recipe/test** I was passing the value like name=test, and that is when in the debugger i could not pick it up, as it was saying value of name=test and got confused. **localhost:8080/recipe/name=test** – Adnan Mian Jul 03 '17 at 18:00
  • @AdnanMian Please post this as an answer and accept it. Accepting is possible after three days I think. This way others can find the answer without hunting through the comments. – Jens Schauder May 04 '21 at 05:51
  • @here Do you have the setter/getter method on your Recipe class, If not try with setter and Getter method – Darshan May 04 '21 at 05:46

1 Answers1

0

I was passing the wrong value of the param to my controller. Instead of: localhost:8080/recipe/test I was passing the value like name=test (localhost:8080/recipe/name=test).

So it was passing the value of the name as "name=test" to recipeRepository.findByName() method instead of "test"

Adnan Mian
  • 415
  • 1
  • 5
  • 17