Spring boot+JPA(hibernate)配置复杂查询的坑

小弟刚从mybatis转到hibernate,遇到了不少坑,在这里不定期更新。以告慰相同处境的亲。 

1、首先hibernate的HQL语法有很多坑,有些在SQL中能执行的语句,在HQL中未必能执行,比如HQL就不支持union操作,可以考虑把你要union的两个查询分开写,然后将结果合起来,传到你要使用union的地方。

2、其次,注意mysql数据库与SqlServer、oracle数据库存在差别的,虽然SQL语法大体差不多,但是在细小的地方还是不同。

比如:MySQL不支持,在增删操作语句中嵌套本表的查询子语句。

delete  from table where id  in(select id  from table where id >10);//会报错啊,兄弟们,提示mysql不支持自身子嵌套。

修改成:

delete  from table where id  in(select id from (select id  from table where id >10) as A);//成功。

3、对于Spring boot+JPA的复杂查询,需要进行如下配置才可以。不然会报如下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainApi': 
Unsatisfied dependency expressed through field 'checkProductStatusService': Error creating bean with name 'checkProductStatusService': 
Unsatisfied dependency expressed through field 'checkProductRepository': Error creating bean with name 'checkProductStatusRepository': 
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 
Validation failed for query for method public abstract java.util.List com.mode.repository.CheckProductStatusRepository.findID2()!;
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'checkProductStatusRepository': 
 Invocation of init method failed; nested exception is java.lang.IllegalArgumentException:
 Validation failed for query for method public abstract java.util.List com.mode.repository.CheckProductStatusRepository.findID2()!;
 nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'checkProductStatusService': 
 Unsatisfied dependency expressed through field 'checkProductRepository': Error creating bean with name 'checkProductStatusRepository': 
 Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 
 Validation failed for query for method public abstract java.util.List com.mode.repository.CheckProductStatusRepository.findID2()!; 
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'checkProductStatusRepository': 
 Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 
Validation failed for query for method public abstract java.util.List com.mode.repository.CheckProductStatusRepository.findID2()!


以delete为例:

@Transactional
public interface CheckProductStatusRepository extends JpaRepository<CheckProductStatus, Long> {

    @Modifying
    @Transactional
    @Query(value = "delete from check_product_status where  id not in (:listParam)", nativeQuery = true)
    void deleteCheckProductStatus(@Param("listParam") List<Integer> listParam);


    @Query(value = "select id from check_product_status  where product_url is not null and product_url <>'' group by product_url", nativeQuery = true)
    List<Integer> findID1();


    @Query(value = "select id from check_product_status where product_url is null or product_url ='' group by spu", nativeQuery = true)
    List<Integer> findID2();
}

猜你喜欢

转载自blog.csdn.net/mxd446814583/article/details/80241330
今日推荐