io.github.taills.common.jpa.service.AbstractService Maven / Gradle / Ivy
package io.github.taills.common.jpa.service;
import io.github.taills.common.jpa.entity.BaseEntity;
import io.github.taills.common.jpa.repository.BaseRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.ParameterizedType;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
/**
* @ClassName AbstractService
* @Description
* @Author nil
* @Date 2021/10/18 11:02 下午
**/
public abstract class AbstractService implements BaseService {
/**
* 泛型注入
*/
@Autowired
protected BaseRepository baseRepository;
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public List findAll() {
return this.baseRepository.findAll();
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public List findAll(Sort sort) {
return this.baseRepository.findAll(sort);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public List findAllById(Iterable ids) {
return this.baseRepository.findAllById(ids);
}
@Override
@Transactional(rollbackFor = Throwable.class)
public List saveAll(Iterable entities) {
return this.baseRepository.saveAll(entities);
}
@Override
@Transactional(rollbackFor = Throwable.class)
public void flush() {
this.baseRepository.flush();
}
@Override
@Transactional(rollbackFor = Throwable.class)
public S saveAndFlush(S entity) {
return this.baseRepository.saveAndFlush(entity);
}
@Override
@Transactional(rollbackFor = Throwable.class)
@Deprecated
public void deleteInBatch(Iterable entities) {
this.baseRepository.deleteInBatch(entities);
}
@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteAllInBatch() {
this.baseRepository.deleteAllInBatch();
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
@Deprecated
public T getOne(ID id) {
return this.baseRepository.getOne(id);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public List findAll(Example example) {
return this.baseRepository.findAll(example);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public List findAll(Example example, Sort sort) {
return this.baseRepository.findAll(example, sort);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public Page findAll(Pageable pageable) {
return this.baseRepository.findAll(pageable);
}
@Override
@Transactional(rollbackFor = Throwable.class)
public S save(S entity) {
return this.baseRepository.save(entity);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public Optional findById(ID id) {
return this.baseRepository.findById(id);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public boolean existsById(ID id) {
return this.baseRepository.existsById(id);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public long count() {
return this.baseRepository.count();
}
@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteById(ID id) {
this.baseRepository.deleteById(id);
}
@Override
@Transactional(rollbackFor = Throwable.class)
public void delete(T entity) {
this.baseRepository.delete(entity);
}
@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteAll(Iterable extends T> entities) {
this.baseRepository.deleteAll(entities);
}
@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteAll() {
this.baseRepository.deleteAll();
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public Optional findOne(Example example) {
return this.baseRepository.findOne(example);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public Page findAll(Example example, Pageable pageable) {
return this.baseRepository.findAll(example, pageable);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public long count(Example example) {
return this.baseRepository.count(example);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public boolean exists(Example example) {
return this.baseRepository.exists(example);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public Optional findOne(Specification spec) {
return this.baseRepository.findOne(spec);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public List findAll(Specification spec) {
return this.baseRepository.findAll(spec);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public Page findAll(Specification spec, Pageable pageable) {
return this.baseRepository.findAll(spec, pageable);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public List findAll(Specification spec, Sort sort) {
return this.baseRepository.findAll(spec, sort);
}
@Override
@Transactional(readOnly = true, rollbackFor = Throwable.class)
public long count(Specification spec) {
return this.baseRepository.count(spec);
}
@Override
public List saveAllAndFlush(Iterable entities) {
return this.baseRepository.saveAllAndFlush(entities);
}
@Override
public void deleteAllInBatch(Iterable entities) {
this.baseRepository.deleteAllInBatch(entities);
}
@Override
public void deleteAllByIdInBatch(Iterable ids) {
this.baseRepository.deleteAllByIdInBatch(ids);
}
@Override
public T getById(ID id) {
return this.baseRepository.getById(id);
}
@Override
public void deleteAllById(Iterable extends ID> ids) {
this.baseRepository.deleteAllById(ids);
}
@Override
public Class getEntityClass() {
Class tClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
return tClass;
}
@Override
public S update(S entity) {
S original = (S) this.baseRepository.getById((ID) entity.getId());
copyNonNullProperties(entity, original);
return this.baseRepository.save(original);
}
public static void copyNonNullProperties(Object src, Object target) {
BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
}
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set emptyNames = new HashSet();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) {
emptyNames.add(pd.getName());
}
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
@Override
public R findBy(Example example, Function, R> queryFunction) {
return this.baseRepository.findBy(example,queryFunction);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy