java query method findAllByCode response all OneToMany objects from db

36 Views Asked by At

I have a several entity classes below

@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;
}

and service which has a method to find all from db with barcodeId from request

@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

UPD 08-03-24

tried to use

Hi! Thanks, but i tried to use

List<Mp> findAllByBarcode = mpRepository.findAllByBarcode(id)
                .stream()
                .filter(m -> m.getMpProductFeedbacks()
                        .stream().iterator().next().getProductVivreBarcodeId().equals(id)).toList();

and

   List<Mp> findAllByBarcode = mpRepository.findAllByBarcode(id)
                .stream()
                .iterator()
                .next()
                .getMpProductFeedbacks()
                .stream()
                .filter(mpProductFeedback -> mpProductFeedback.getProductVivreBarcodeId().equals(id)).collect(Collectors.toList());

and this two doesnt work(


but this one response all rows from db which include rows where barcodeId=id and barcodeId != id

HOW to filter to fix this problem? i need rows only with id from reques

stream.filter fori cycle

1

There are 1 best solutions below

1
guapi On

Maybe you should improve your findAllByBarcode method instead of find all result and use stream to filter it :).

This is my code, you can try to run

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

It may be an another choice

List<Mp> filteredMps = allMps.stream()
            .filter(mp -> mp.getMpProductFeedbacks().stream()
                    .anyMatch(feedback -> feedback.getBarcodeId().equals(id)))
            .collect(Collectors.toList());