我正在从事一个恢复的春季启动项目,该项目从mysql db中获取数据。
我只想打印所有具有等于1的活动字段的类别我想将其应用于类别中的所有方法: findAll
, findByParentId
..ETC。
package com.userService.repositories;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.userService.entities.Category;
public interface CategoryRepo extends JpaRepository<Category, Integer> {
@Query("where active =1")
public List<Category> findByParentId(int id);
}
我尝试使用查询方法,但它不起作用,并给了我例外
如果您将Hibernate用作持久性提供者,则可以利用实体级别上的@Where
子句:
@Where(clause = "active =1")
@Entity
public class Category{
这将应用于通过持久提供商的所有查询。
,如果您使用查询方法,则应该指定
,这可能会有所帮助 select alias_name from Category c where condition
或
from Category where condition
或
直接使用方法
findByActive(int id);
public interface CategoryRepo extends JpaRepository<Category, Integer> {
@Query("select c from Category c where c.active =1")
public List<Category> findByParentId(int id);
}
@Query
注释允许执行本机查询。因此,我认为,您应该指定完整的SQL如下
@Query("select c from Category c where c.active =1")