JPA findBy(属性) return 空
JPA findBy(Property) return null
我在 Spring 引导中编写了 JPA 代码,我想在其中对实体执行 CRUD 和其他操作,我编写了扩展 JpaRepository
的 RecipeRepository
public interface RecipeRepository extends JpaRepository<Recipe,Long> {
public List<Recipe> findByName(String name);
public Recipe findOneByName(String name);
}
实体class是;
@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;
..
}
当我调用 recipeRepository.findByName("test") 或 recipeRepository.findOneByName("test") ,我得到 null。当我调用 findAll() 然后对值进行处理时,我可以找到名称为 test
的 Recipe
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
}
在 findByName 或 findOneByName 的日志中,我在日志中得到以下内容:
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=?
我将错误的参数值传递给我的控制器。而不是: localhost:8080/recipe/test 我传递的值像 name=test (localhost:8080 /recipe/name=测试).
所以它将名称的值作为“name=test”传递给 recipeRepository.findByName() 方法,而不是 “test”。 =19=]
我在 Spring 引导中编写了 JPA 代码,我想在其中对实体执行 CRUD 和其他操作,我编写了扩展 JpaRepository
的 RecipeRepositorypublic interface RecipeRepository extends JpaRepository<Recipe,Long> {
public List<Recipe> findByName(String name);
public Recipe findOneByName(String name);
}
实体class是;
@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;
..
}
当我调用 recipeRepository.findByName("test") 或 recipeRepository.findOneByName("test") ,我得到 null。当我调用 findAll() 然后对值进行处理时,我可以找到名称为 test
的 RecipeString 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
}
在 findByName 或 findOneByName 的日志中,我在日志中得到以下内容:
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=?
我将错误的参数值传递给我的控制器。而不是: localhost:8080/recipe/test 我传递的值像 name=test (localhost:8080 /recipe/name=测试).
所以它将名称的值作为“name=test”传递给 recipeRepository.findByName() 方法,而不是 “test”。 =19=]