|
|
@ -15,6 +15,7 @@ import org.springframework.data.repository.core.EntityInformation; |
|
|
|
import org.springframework.data.repository.support.Repositories; |
|
|
|
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
import javax.persistence.criteria.*; |
|
|
|
import javax.persistence.metamodel.PluralAttribute; |
|
|
@ -34,7 +35,7 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
public R getRepository() { |
|
|
|
return repository; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public DaoExceptionProvider<E, ID> getExceptionProvider() { |
|
|
|
return new DaoExceptionProvider<E, ID>(){}; |
|
|
@ -60,6 +61,11 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String[] getDefaultSortBy() { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
private void standardizeSingularAssociationAttributes(DirectFieldAccessFallbackBeanWrapper wrapper, ManagedTypeAttributes managedTypeAttributes){ |
|
|
|
Map<String,SingularAttribute<?,?>> attributes =managedTypeAttributes.getSingularAssociationAttributes(); |
|
|
|
if(attributes==null || attributes.isEmpty()){ return;} |
|
|
@ -110,7 +116,7 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
standardizeSingularAssociationAttributes(wrapper,managedTypeAttributes); |
|
|
|
standardizePluralAssociationAttributes(wrapper,managedTypeAttributes); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void copyEntityValues(E oldEntity,E newEntity){ |
|
|
|
DirectFieldAccessFallbackBeanWrapper oldEntityWrapper = new DirectFieldAccessFallbackBeanWrapper(oldEntity); |
|
|
|
DirectFieldAccessFallbackBeanWrapper newEntityWrapper = new DirectFieldAccessFallbackBeanWrapper(newEntity); |
|
|
@ -217,7 +223,7 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
} |
|
|
|
return Collections.emptyList(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public E remove(E entity) throws Exception { |
|
|
@ -233,7 +239,7 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
public void removeAll() throws Exception { |
|
|
|
repository.deleteAllInBatch(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public E update(ID id, E entity) throws Exception { |
|
|
@ -353,6 +359,7 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
@Override |
|
|
|
public List<E> list(Specification<E> specification, QueryParameter queryParameter) throws Exception { |
|
|
|
if(queryParameter!=null) { |
|
|
|
setDefaultSortBy(queryParameter); |
|
|
|
Sort sort=queryParameter.getSort(); |
|
|
|
if(sort!=null){ |
|
|
|
if(specification!=null) { |
|
|
@ -383,6 +390,7 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
@Override |
|
|
|
public Page<E> query(Specification<E> specification, QueryParameter queryParameter) throws Exception { |
|
|
|
if(queryParameter!=null) { |
|
|
|
setDefaultSortBy(queryParameter); |
|
|
|
if(queryParameter.getPageable()){ |
|
|
|
Pageable pageable =queryParameter.getJpaPageable(); |
|
|
|
if(specification!=null) { |
|
|
@ -410,6 +418,22 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
return QueryResult.emptyPage(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<E> getDefaultValues() throws Exception{ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public void resetDefaultValues() throws Exception { |
|
|
|
List<E> values =getDefaultValues(); |
|
|
|
if(values!=null && values.size()>0){ |
|
|
|
repository.deleteAll(); |
|
|
|
repository.flush(); |
|
|
|
repository.saveAll(values); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 重新 new 一个被删除的实体对象 |
|
|
|
* 当一个实体被删除后,该实体将从 JPA Session 中脱离,在返回给客户端的 JSON 串时,可能涉及从实体中获取其他关联对象,此时就会出现 Session 状态错误 |
|
|
@ -425,4 +449,17 @@ public abstract class DaoServiceImpl<E,ID extends Serializable,R extends DaoRepo |
|
|
|
repository.setId(obj,repository.getId(entity)); |
|
|
|
return obj; |
|
|
|
} |
|
|
|
|
|
|
|
private void setDefaultSortBy(QueryParameter queryParameter){ |
|
|
|
if (!queryParameter.existsSortBy()) { // 没有指定的排序器才使用默认排序器
|
|
|
|
String[] defaultSortBys = this.getDefaultSortBy(); |
|
|
|
if (defaultSortBys != null && defaultSortBys.length > 0) { |
|
|
|
for (String sortBy : defaultSortBys) { |
|
|
|
if (StringUtils.hasText(sortBy)) { |
|
|
|
queryParameter.addSortBy(sortBy.trim()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|