yun-zuoyi
许心洁 6 years ago
parent 56a4d6b362
commit 057fa88904

@ -10,7 +10,6 @@ import org.hibernate.NonUniqueResultException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.util.CollectionUtils;
import javax.persistence.*; import javax.persistence.*;
import java.io.Serializable; import java.io.Serializable;
@ -219,7 +218,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
/** /**
* *
*
* @param conditionName * @param conditionName
* @param conditionValue * @param conditionValue
* @param propertyName * @param propertyName
@ -480,22 +478,25 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
@Override @Override
public T getByProperty(DdlPackBean packBean) { public T getByProperty(DdlPackBean packBean) {
try {
List<T> list = findByHqlWhere(packBean); List<T> list = findByHqlWhere(packBean);
if (CollectionUtils.isEmpty(list)) { return list.size() != 0 ? list.get(0) : null;
}catch(NoResultException ne){
LOGGER.error("数据不存在DdlPackBean{}",packBean); LOGGER.error("数据不存在DdlPackBean{}",packBean);
throw new RuntimeException("数据不存在"); return null;
} else if (list.size() > 1) { }catch(NonUniqueResultException ex){
LOGGER.error("查询单条记录但出现多条。packBean{}",packBean); LOGGER.error("查询单条记录但出现多条。packBean{}",packBean);
throw new RuntimeException("存在多条记录"); throw new RuntimeException("存在多条记录" + ex.getMessage());
} }
return list.iterator().next();
} }
@Override @Override
public T getByProperty(String propertyName, Object value) { public T getByProperty(String propertyName, Object value) {
String queryString = "from " + persistentClass.getSimpleName() + " as model where model." + propertyName + "= :" + propertyName; String queryString = "from " + persistentClass.getSimpleName() + " as model where model." + propertyName + "= :" + propertyName;
try { try {
return (T) entityManager.createQuery(queryString).setParameter(propertyName, value).getSingleResult(); List<T> list = entityManager.createQuery(queryString).setParameter(propertyName, value).getResultList();
return list.size() != 0 ? list.get(0) : null;
}catch(NoResultException ne){ }catch(NoResultException ne){
LOGGER.error("数据不存在prop{},value{}",propertyName,value,ne); LOGGER.error("数据不存在prop{},value{}",propertyName,value,ne);
return null; return null;
@ -532,7 +533,8 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
} }
try{ try{
return (T) queryObject.getSingleResult(); List<T> list = queryObject.getResultList();
return list.size() != 0 ? list.get(0) : null;
}catch(NoResultException ne){ }catch(NoResultException ne){
LOGGER.error("数据不存在",ne); LOGGER.error("数据不存在",ne);
return null; return null;
@ -1148,7 +1150,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
/** /**
* *
*
* @param item * @param item
* @return * @return
*/ */
@ -1212,7 +1213,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
/** /**
* *
*
* @param field * @param field
*/ */
private void fieldSetInitParam(T item,Field field) { private void fieldSetInitParam(T item,Field field) {
@ -1268,7 +1268,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
/** /**
* *
*
* @return * @return
*/ */
@Override @Override
@ -1291,7 +1290,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
/** /**
* *
*
* @return * @return
*/ */
@Override @Override
@ -1330,7 +1328,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
/** /**
* *
*
* @return * @return
*/ */
@Override @Override
@ -1353,7 +1350,6 @@ public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRep
/** /**
* *
*
* @return * @return
*/ */
@Override @Override

Loading…
Cancel
Save