【问题标题】:how to convert hibernate query to criteria如何将休眠查询转换为条件
【发布时间】:2019-07-05 20:21:20
【问题描述】:

我已经编写了一个遍历多个实体的查询,并希望将其转换为分离条件查询。不知道如何为多个表执行此操作

StringBuilder queryString = new StringBuilder("from ");
    queryString.append(Entity).append(" e where e.specificationId in (:ids) 
       and not exists ( select 1 from Association a where "
           + "a.sourceReference = e.reference )").append("order by    
                e.displayOrder");

【问题讨论】:

  • 你可以使用 Spring JPA。

标签: hibernate hql criteria


【解决方案1】:

您可以使用 Spring JPA。

@NoRepositoryBean
public interface GenericRepository<T extends Content<T>> extends JpaRepository<T, Long>

@Query("select e from #{#entityName} e where e.specificationId in (?) and not exists ( select 1 from Association a where a.sourceReference = e.reference ) order by e.displayOrder", nativeQuery=true)
public void T findByIds(List<Long> ids);

您将为每种类型创建一个接口并实现 GenericRepository。

例如:

@Repository
public interface AbcRepository extends GenericRepository<Abc>{

}

参考:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 2023-04-01
    • 2018-12-19
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多