我已经写了一个查询通过多个实体循环,并想将它转化成一种超脱的条件查询。不知道如何做到这一点的多个表
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。
@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/