【问题标题】:How to set criteria fetch mode using specification in spring jpa?如何使用spring jpa中的规范设置标准获取模式?
【发布时间】:2021-07-04 18:29:39
【问题描述】:

我需要实现一个动态查询,为此我正在使用 org.springframework.data.jpa.domain.Specification 接口。 例如:

 public class PlayerSpecification {

   public static Specification<Player> name(String name{
        return (root, criteraQuery, criteriaBuilder)-> 
            criteriaBuilder.equal(root.get("name"), name);
    }

    public static Specification<Player> teamName (String teamName){
        return (root, criteraQuery, criteriaBuilder)-> 
            criteriaBuilder.equal(root.get("team").get("name"), teamName);
    }
}

我想在 Criteria 中设置 JOIN FETCH。 比如:

criteraQuery.setFetchMode("teams", FetchMode.EAGER);

有人知道怎么做吗?

【问题讨论】:

    标签: jpa spring-data-jpa hibernate-criteria


    【解决方案1】:

    我不确定实体的外观,但应该是:

    root.fetch( "teams" );
    

    请参阅 Hibernate ORM fetch example in the documentation

    【讨论】:

    • 代码在哪里,因为 root 已经可用?
    • 我不知道,这取决于你在哪里需要它。你想写什么样的查询?
    猜你喜欢
    • 2010-10-09
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 2019-08-26
    • 2018-06-15
    • 2018-09-06
    相关资源
    最近更新 更多