1

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=?

4

1 回答 1

0

我将参数的错误值传递给我的控制器。而不是:localhost:8080/recipe/test我传递的值类似于name=test (localhost:8080/recipe/name=test)

所以它将名称的值作为“ name=test ”传递给 recipeRepository.findByName() 方法而不是“test”

于 2021-07-10T16:33:27.200 回答