package kr.wisestone.owl.service.impl; import kr.wisestone.owl.common.MessageAccessor; import kr.wisestone.owl.service.AbstractService; import kr.wisestone.owl.util.WebAppUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.io.Serializable; import java.util.List; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; public abstract class AbstractServiceImpl> implements AbstractService { protected abstract JpaRepository getRepository(); @PersistenceContext protected EntityManager entityManager; @Autowired protected MessageAccessor messageAccessor; @Autowired protected WebAppUtil webAppUtil; private static final int BATCH_COUNT = 200; // 배치 사이즈 @Override public long count() { return this.getRepository().count(); } @Override public void clear() { this.entityManager.clear(); } @Override public void detach(Object entity) { this.entityManager.detach(entity); } @Override @Transactional public void bulkInsert(List entities) { AtomicInteger adGroupInsightIndex = new AtomicInteger(1); entities.forEach(entity -> { entity = this.entityManager.merge(entity); if (adGroupInsightIndex.get() > 0 && adGroupInsightIndex.get() % BATCH_COUNT == 0) { this.entityManager.flush(); this.entityManager.clear(); } adGroupInsightIndex.getAndIncrement(); }); entityManager.flush(); entityManager.clear(); } @Override @Transactional(readOnly = true) public T findOne(ID id) { Optional entity = this.getRepository().findById(id); if (entity.isPresent()) { return entity.get(); } return null; } /*@Override @Transactional(readOnly = true) public T findOne(ID id) { return this.getRepository().getOne(id); }*/ @Override @Transactional(readOnly = true) public List findAll(List ids) { return this.getRepository().findAllById(ids); } }