Spring JPA连表查询重复关联如何解决?

技术百科 聖光之護 发布时间:2024-12-01 浏览:

spring jpa 连表重复 join 解决方案

为了解决 userentityuserarchiveentity 之间重复的 userarchive 关联,可以在 specification 中手动指定连接类型。修改后的代码如下:

specification specification = (root, query, criteriabuilder) -> {
    predicate predicate = criteriabuilder.conjunction();
    join userarchiveentityjoin = root.join("userarchive", jointype.left);
    return predicate;
};

对于不同的业务场景需要不同的模型关联,spring jpa 无法提供针对不同关联要求的方法重载。可以使用依赖注入来解决此问题:

@Repository
public class UserRepositoryImpl implements UserRepositoryCustom {

    @Autowired
    private UserRepository userRepository;

    @Override
    public Page findAllWithUserArchive(Specification specification, Pageable pageable) {
        EntityGraph entityGraph = EntityGraph.of(UserEntity.class)
                .addSubgraph("userArchive");
        return userRepository.findAll(specification, pageable, entityGraph);
    }

    @Override
    public Page findAllWithUserArchiveAndNation(Specification specification, Pageable pageable) {
        EntityGraph entityGraph = EntityGraph.of(UserEntity.class)
                .addSubgraph("userArchive")
                .addSubgraph("userArchive.nation");
        return userRepository.findAll(specification, pageable, entityGraph);
    }
}


# 可以使用  # spring  # left  # query  # implements  # userarchiveentityjoin  # jointype  # conjunction  # criteriabuilder  # predicate  # UserRepositoryImpl 


相关栏目: <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 AI推广<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 SEO优化<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 技术百科<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 谷歌推广<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 百度推广<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 网络营销<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 案例网站<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 精选文章<?muma echo $count; ?>

相关推荐

在线咨询

点击这里给我发消息QQ客服

在线咨询

免费通话

24h咨询:4006964355


如您有问题,可以咨询我们的24H咨询电话!

免费通话

微信扫一扫

微信联系
返回顶部