com.kasinf.framework.rest.repository.BaseTreeRepositoryImpl Maven / Gradle / Ivy
package com.kasinf.framework.rest.repository;
import com.kasinf.framework.rest.eneity.AbstractEntity;
import com.kasinf.framework.rest.eneity.type.LogicDeletable;
import com.kasinf.framework.rest.eneity.type.TreeEntity;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.util.Assert;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.io.Serializable;
/**
* @author lkhsh
* @param
* @param
*/
public class BaseTreeRepositoryImpl extends BaseRepositoryImpl {
private final String DELETE_CHILDREN_QL;
private final String LOGIC_DELETE_CHILDREN_QL;
public BaseTreeRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
DELETE_CHILDREN_QL = String.format("delete from %s x where x.parentIds like concat(:parentIds, %s)", entityName, "'%'");
LOGIC_DELETE_CHILDREN_QL = String.format("update %s x set x.isDelete=true where x.parentIds like concat(:parentIds, %s)", entityName, "'%'");
}
@Override
@Transactional(rollbackOn = Exception.class)
public void delete(T t) {
Assert.notNull(t, "Entity must not be null!");
Query query;
if (t instanceof LogicDeletable) {
query = em.createQuery(LOGIC_DELETE_CHILDREN_QL);
} else {
query = em.createQuery(DELETE_CHILDREN_QL);
}
query.setParameter("parentIds",t.getParentIds());
query.executeUpdate();
}
@Override
public void deleteInBatch(Iterable entities) {
entities.forEach(this::delete);
}
}