package com.eactive.eai.rms.data.entity.onl.authserver;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.eactive.eai.data.entity.onl.authserver.QScopeEntity;
import com.eactive.eai.data.entity.onl.authserver.ScopeEntity;
import com.eactive.eai.data.entity.onl.authserver.ScopeEntityId;
import com.eactive.eai.data.jpa.AbstractDataService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ScopeEntityService extends AbstractDataService<ScopeEntity, ScopeEntityId, ScopeEntityEMSRepository> {
	
	@PersistenceContext
    private EntityManager entityManager; // AbstractDataService 의 entityManager가 private이라 접근 불가하여 별도 주입
	
	public void deleteByIdScopeId(String scopeId) {
		getJPAQueryFactory().delete(QScopeEntity.scopeEntity)
									 .where(QScopeEntity.scopeEntity.id.scopeid.eq(scopeId))
									 .execute();
		
		// 동일 트랜잭션 내에서 같은 ID로 재저장 시 충돌(EntityExistsException)을 방지
		entityManager.flush(); 
	    entityManager.clear();
	}

}
