java查询方法findAllByCode响应来自db的所有OneToMany对象

问题描述 投票:0回答:1

我有下面几个实体类

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "mp")
public class Mp {



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

    @Column(name = "web_source_name")
    private String webSourceName;

    @Column(name = "web_source_link")
    private String webSourceLink;

    @OneToMany(mappedBy = "mpId", fetch = FetchType.LAZY)
    private List<MpProductFeedback> mpProductFeedbacks;
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "mp_product_feedback")
public class MpProductFeedback {



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

    @Column(name = "product_id")
    private String productId;

    @Column(name = "barcodeId_id")
    private Integer barcodeId;

    @Column(name = "mp_id")
    private Integer mpId;

    @Column(name = "avg_score")
    private BigDecimal avgScore;

    @Column(name = "avg_sum")
    private BigDecimal avgSum;

    @OneToMany(mappedBy = "mpProductFeedbackId", fetch = FetchType.LAZY)
    private List<MpPersonScore> mpPersonScores;
    
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "mp_person_score")
public class MpPersonScore {



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

    @Column(name = "product_id")
    private String productId; // пока непойму зачем мне это поле

    @Column(name = "barcode_id")
    private Integer BarcodeId;

    @Column(name = "mp_id")
    private Integer mpId;

    @Column(name = "mp_product_feedback_id")
    private Long mpProductFeedbackId;

    @Column(name = "mp_person_login")
    private String mpPersonLogin;

    @Column(name = "mp_person_score")
    private Integer mpPersonScore;

    @Column(name = "mp_person_comment")
    private String mpPersonComment;

    @OneToMany(mappedBy = "mpPersonScoreId", fetch = FetchType.LAZY)
    private List<PersonLinkPhoto> personLinkPhotos;

}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "person_link_photo")
public class PersonLinkPhoto {

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

    @Column(name = "mp_person_login")
    private String mpPersonLogin;

    @Column(name = "link")
    private String link;

    @Column(name = "mp_person_score_id")
    private Long mpPersonScoreId;
}

服务有一个方法可以从数据库中查找所有带有请求中的barcodeId的内容

@Service
@RequiredArgsConstructor
public class PersonRegisterServiceImpl implements PersonRegisterService{

    private final PersonRepository personRepository;
    private final MpProductFeedbackRepository mpProductFeedbackRepository;
    private final MpPersonScoreRepository mpPersonScoreRepository;
    private final MpRepository mpRepository;
    private final PersonLinkPhotoRepository personLinkPhotoRepository;

    @Override
    @Transactional
    public List<FeedbackPerBarcodeResponse> feedbackPerBarcodeV1(Integer id) {

        List<Mp> findAllByBarcode = mpRepository.findAllByBarcode(id); // this one - multidimentional list

但这一个响应数据库中的所有行,其中包括barcodeId=id 和barcodeId != id 的行

如何过滤来解决这个问题?我只需要带有请求中 id 的行

stream.filter 福里循环

java one-to-many psql findall
1个回答
0
投票

也许你应该改进你的

findAllByBarcode
方法,而不是查找所有结果并使用流来过滤它:)。

这是我的代码,你可以尝试运行

List<Mp> filteredList = mpRepository.findAllByBarcode(id)
                                .stream()
                                .filter(mp -> mp.getId().equals(id))
                                .collect(Collectors.toList());

可能是另一种选择

List<Mp> filteredMps = allMps.stream()
            .filter(mp -> mp.getMpProductFeedbacks().stream()
                    .anyMatch(feedback -> feedback.getBarcodeId().equals(id)))
            .collect(Collectors.toList());
© www.soinside.com 2019 - 2024. All rights reserved.