Progr1mmer 6 år sedan
förälder
incheckning
b60f9f123d
1 ändrade filer med 21 tillägg och 0 borttagningar
  1. 21 0
      mysql-starter/src/main/java/com/yihu/mysql/query/BaseJpaService.java

+ 21 - 0
mysql-starter/src/main/java/com/yihu/mysql/query/BaseJpaService.java

@ -10,6 +10,7 @@ import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@ -81,6 +82,26 @@ public class BaseJpaService<T, R> {
    }
    public int delete(Object[] ids){
        if (repoClass != null) {
            Type [] types = repoClass.getGenericInterfaces();
            for (Type type : types) {
                if (type.getClass().equals(ParameterizedTypeImpl.class)) {
                    ParameterizedType _type = (ParameterizedType)type;
                    if (_type.getRawType().getTypeName().equals(PagingAndSortingRepository.class.getName())) {
                        Type [] actualTypeArguments = _type.getActualTypeArguments();
                        if (actualTypeArguments.length == 2) {
                            if (actualTypeArguments[1].getTypeName().equals(Integer.class.getName())) {
                                Integer [] _ids = new Integer[ids.length];
                                for (int i = 0; i < ids.length; i ++) {
                                    _ids[i] = new Integer(ids[i].toString());
                                }
                                ids = _ids;
                            }
                        }
                    }
                }
            }
        }
        String hql = " DELETE FROM " + getEntityClass().getName() + " WHERE " + getEntityIdFiled() + " in(:ids)";
        Query query = currentSession().createQuery(hql);
        query.setParameterList("ids", ids);