【发布时间】:2020-04-15 11:32:36
【问题描述】:
我正在尝试编写一个本机查询来从基于 EnumType 实体的表中进行搜索。此 ENUM MealType 是 @Table Meal 的一部分。
@Column(name = "meal_type")
@Enumerated(EnumType.STRING)
private MealType mealType;
现在,我的查询是:
@Repository
public interface MealRepository extends JpaRepository<Meal, Long> {
@Query(value ="select * from meal m where m.meal_type = ?1", nativeQuery = true)
List<Meal> findMealByType(MealType mealType);
}
但是当我对其进行测试时,我不断收到org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet
除此之外,我还尝试使用 MealType 作为参数重写查询:
@Query(value ="select * from meal m where m.meal_type in :meal_type ", nativeQuery = true)
List<Meal> findMealByType(@Param("meal_type") MealType mealType);
但它导致了另一种错误
InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select * from meal m where m.meal_type in ? ]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
我预计其他地方会出现问题,但是基于 ID 搜索的相同自定义查询可以正常工作。
【问题讨论】:
-
你试过
List<Meal> findMealByType(String mealType);吗? -
但我需要将 Enum 作为参数传递
-
@VladDemyan 你为什么使用
nativeQuery? -
你用的是spring data jpa,为什么不
findByMealType(MealType mealType);没什么好补充的。
标签: java spring jpa spring-data-jpa