Spring Batch: автоматическая фиксация в ItemProcessor? - PullRequest
0 голосов
/ 20 апреля 2020

У меня есть несколько кодов в моем проекте, и я вижу строку this.entityManager.detach (нутриент); так Spring Batch это автоматическая фиксация на ItemProcessor? И ItemProcessor находится в транзакции, то же самое с Itemreader или Itemwrite ?? Может кто-нибудь помочь мне, почему с помощью detach, я новичок. если есть пример, это здорово, спасибо большое.

public class NutrientDtoToNutrient implements ItemProcessor<NutrientDto, Nutrient>, StepExecutionListener {

//// some @Autowied ...

@Override
    public Nutrient process(NutrientDto nutrientDto) throws Exception {

        Nutrient nutrient;

        // See if this nutrient is already stored in the DB.
        nutrient = this.nutrientRepository.findByGenesisIdAndSourceSystem(
                String.valueOf(nutrientDto.getId()), this.sourceSystem.getId());

        // If not, create a new one.
        if (nutrient == null) {
            return this.toNutrient(nutrientDto);
        } else {
            // Detach the nutrient from the entity manager to keep Hibernate from automatically updating
            // the DB.
            this.entityManager.detach(nutrient);
            return this.updateNutrient(nutrient, nutrientDto) ? nutrient : null;
        }
    }

public class NutrientWriter implements ItemWriter<Nutrient> {
    @Override
    public void write(List<? extends Nutrient> nutrients) throws Exception {

        if (nutrients.isEmpty()) {
            NutrientWriter.logger.info(EMPTY_LIST_MESSAGE);
            return;
        }
        this.nutrientPersister.saveNutrients(nutrients);
    }



////in file xml
<batch:job id="I18X001W">
<batch:step id="I18X001W-STEP-1">
                <batch:tasklet>
                    <batch:chunk reader="nutrientReader" processor="nutrientProcessor" writer="nutrientWriter"
                                 commit-interval="10"/>
                </batch:tasklet>
            </batch:step>
 </batch:job>
...