Home > Back-end >  Iterate over a java List
Iterate over a java List

Time:03-31

I have these entities: ReleveBancaireEntity LigneReleveEntity OperationCreditEntity and these transactions entities OperationChequeEntity OperationVirementEntity and OperationEspeceEntity

  • So my ReleveBancaireEntity has a list of LigneReleveEntity and OperationCreditEntity has inheritance of type single table and all transactions entities extends from OperationCreditEntity, my goal is to verify and filter LigneReleveEntity based on a field called private String creditDebit; and see if it has letter D I should ignore it and don't persist it, and if it has letter C I want to persist it to the database OperationCreditEntity Here are my entites:

ReleveBancaireEntity

public class ReleveBancaireEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long releveBancaireId;
    @CreationTimestamp
    @Temporal(TemporalType.DATE)
    private Date dateReception;
    private String label;
    @OneToMany(cascade= CascadeType.ALL,mappedBy = "releveBancaire", fetch = FetchType.EAGER)
    private List<LigneReleveEntity> lignereleve = new ArrayList<>();
}

LigneReleveEntity

public class LigneReleveEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long ligneReleveId;
    @CreationTimestamp
    @Temporal(TemporalType.DATE)
    private Date dateOperation;
    private String operationNature;
    private BigDecimal montant;
    private String creditDebit;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FK_releveBancaire", referencedColumnName = "releveBancaireId")
    private ReleveBancaireEntity releveBancaire;
}

OperationCreditEntity

public class OperationCreditEntity {
    @Id
    @Column(nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long operationCreditId;
    @CreatedDate
    private Date operationDate;
    @OneToOne
    private LigneReleveEntity ligneReleve;
}

OperationEspecesEntity

@DiscriminatorValue("Operation_Especes")
public class OperationEspecesEntity extends OperationCreditEntity {
    private String cin;
    private String nomEmetteur;
    private String prenomEmetteur;
}

OperationVirementEntity

@DiscriminatorValue("Operation_Virement")
public class OperationVirementEntity extends OperationCreditEntity {
    private String rib;
}

OperationChequeEntity

@DiscriminatorValue("Operation_Cheque")
public class OperationChequeEntity extends OperationCreditEntity{
    private int numeroCheque;
}

This is how OperationCreditEntity Table looks like:

enter image description here

This is what I tried:

public void qualification(Long releveBancaireId){
    List<LigneReleveEntity> ligneReleveEntities  = new ArrayList<>();
    OperationCreditEntity operationCreditEntity = new OperationCreditEntity();
    LigneReleveEntity ligneReleve = new LigneReleveEntity();
    Predicate<LigneReleveEntity> byCredit = credit -> Objects.equals(credit.getCreditDebit(), "C");
    if (!Objects.equals(ligneReleve.getCreditDebit(), "C")){
        throw new RuntimeException("Ignoring ...");
    }
    ligneReleveEntities.stream().filter(byCredit).collect(Collectors.toList());
    operationCreditRepository.save(operationCreditEntity);
}

CodePudding user response:

this is what I tried :

public void qualification(Long releveBancaireId){
        List<LigneReleveEntity> ligneReleveEntities  = new ArrayList<>();
        OperationCreditEntity operationCreditEntity = new OperationCreditEntity();
        LigneReleveEntity ligneReleve = new LigneReleveEntity();
        Predicate<LigneReleveEntity> byCredit = credit -> Objects.equals(credit.getCreditDebit(), "C");
        if (!Objects.equals(ligneReleve.getCreditDebit(), "C")){
            throw new RuntimeException("Ignoring ...");
        }
        ligneReleveEntities.stream().filter(byCredit).collect(Collectors.toList());
        operationCreditRepository.save(operationCreditEntity);
    }

CodePudding user response:

I think what you want is something like this:

public void qualification(Long releveBancaireId){

    ReleveBancaireEntity releveBancaire = ...; // find entity by id
    List<OperationCreditEntity> operationsCredit =  releveBancaire.getLignereleve()
        .stream()
        .filter(ligne -> "C".equals(ligne.getCreditDebit())
        .map(ligne -> /* map to OperationCreditEntity here */)
        .collect(Collectors.toList());
    operationCreditRepository.save(operationsCredit);
}
  • Related