Spring Data JPA& QueryDSL:过滤可选的相关实体

时间:2016-04-25 17:06:11

标签: spring jpa querydsl

我在QueryDSL中遇到一个问题。

我使用Spring Data JPA和QueryDslPredicateExecutor来搜索满足特定过滤器的实体,在这种情况下,操作处于特定状态且没有依赖关系或所有依赖关系都满足。

这是我的简化实体:

@Entity
@Table(name = "movie_file_actions")
public class MovieFileAction {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long                    id;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private EActionStatus           status              = EActionStatus.QUEUED;

    @ManyToOne(optional = true)
    private MovieFileAction         parentAction        = null;

    // ...

}

@Generated("com.mysema.query.codegen.EntitySerializer")
public class QMovieFileAction extends EntityPathBase<MovieFileAction> {

    public static final QMovieFileAction movieFileAction = new QMovieFileAction("movieFileAction");

    public final NumberPath<Long> id = createNumber("id", Long.class);

    public final QMovieFile movieFile;

    public final QMovieFileAction parentAction;

    // ...

}

这是我的服务和相关的Spring Data JPA存储库:

@Repository
public interface MovieFileActionRepository extends QueryDslPredicateExecutor<MovieFileAction>, PagingAndSortingRepository<MovieFileAction, Long> {}

@Service
@Transactional
public class MovieFileActionService {

    @Autowired
    MovieFileActionRepository mMovieFileActionRepository;

    public Page<MovieFileAction> findProcessable(Pageable page) {
        QMovieFileAction qMovieFileAction = QMovieFileAction.movieFileAction;

        BooleanExpression prdHasNoDependencies = qMovieFileAction.parentAction.isNull();
        BooleanExpression prdAreDependenciesSatisfied = qMovieFileAction.parentAction.status.eq( EActionStatus.SUCCESS );

        BooleanExpression predicate = qMovieFileAction.status.eq( EActionStatus.QUEUED )
            .and( prdHasNoDependencies.or( prdAreDependenciesSatisfied ) );

        return mMovieFileActionRepository.findAll( predicate, page );
    }

}

当我尝试调用findProcessable方法时,我希望获得所有MovieFileAction s,这些select count(movieFileAction) from MovieFileAction movieFileAction where movieFileAction.status = ?1 and (movieFileAction.parentAction is null or movieFileAction.parentAction.status = ?2) 不依赖于任何东西或者它们的依赖性满足。

生成的HQL查询以检查是否存在任何此类操作似乎对我来说是正确的:

select count(moviefilea0_.id) as col_0_0_ 
from movie_file_actions moviefilea0_ 
cross join movie_file_actions moviefilea1_ 
where moviefilea0_.parent_action=moviefilea1_.id 
and moviefilea0_.status=? and (moviefilea0_.parent_action is null or moviefilea1_.status=?)

然而,生成的SQL不是我的预期:

_.debounce(function() {
    saveParameters({ value: i }, $scope.model);
}, 1000)();

请注意生成的查询中的交叉连接,它将过滤掉没有父项的操作。为什么十字架会加入那里?如何告诉JPA parentAction是可选的,以便生成左连接?

由于

0 个答案:

没有答案