SpringBoot JPA 일괄 처리

1100 단어 JPAJavaWhy
/**
*     
*
*/
public interface JpaBatch {
}


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

public abstract class JpaInsertBatch implements JpaBatch {

    private static final Logger logger = LoggerFactory.getLogger(JpaInsertBatch.class);

    @PersistenceContext
    protected EntityManager em;

    @Transactional
    public void batchInsert(List list) {
        try {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                em.persist(list.get(i));
                if (i % 1000 == 0 || i == (size - 1)) {//       
                    em.flush();
                    em.clear();
                }
            }
            logger.info("    ,  {}   ", list.size());
        } catch (Exception e) {
            logger.error("      ");
            e.printStackTrace();
        }
    }
}

좋은 웹페이지 즐겨찾기