|
@@ -21,6 +21,7 @@ package org.hswebframework.web.service;
|
|
|
import org.hswebframework.web.commons.entity.GenericEntity;
|
|
|
import org.hswebframework.web.commons.entity.RecordCreationEntity;
|
|
|
import org.hswebframework.web.dao.CrudDao;
|
|
|
+import org.hswebframework.web.id.IDGenerator;
|
|
|
import org.hswebframwork.utils.ClassUtils;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
@@ -41,6 +42,8 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
|
|
|
super();
|
|
|
}
|
|
|
|
|
|
+ protected abstract IDGenerator<PK> getIDGenerator();
|
|
|
+
|
|
|
@Override
|
|
|
public abstract CrudDao<E, PK> getDao();
|
|
|
|
|
@@ -52,16 +55,21 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public int updateByPk(E entity) {
|
|
|
+ public int updateByPk(PK pk, E entity) {
|
|
|
+ entity.setId(pk);
|
|
|
tryValidate(entity);
|
|
|
return createUpdate(entity)
|
|
|
//如果是RecordCreationEntity则不修改creator_id和creator_time
|
|
|
.when(ClassUtils.instanceOf(getEntityType(), RecordCreationEntity.class),
|
|
|
- update -> update.and().excludes("creator_id", "creator_time"))
|
|
|
- .where(GenericEntity.id, entity.getId())
|
|
|
+ update -> update.and().excludes(RecordCreationEntity.creatorId, RecordCreationEntity.createTime))
|
|
|
+ .where(GenericEntity.id, pk)
|
|
|
.exec();
|
|
|
}
|
|
|
|
|
|
+ protected int updateByPk(E entity) {
|
|
|
+ return updateByPk(entity.getId(), entity);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public int updateByPk(List<E> data) {
|
|
|
return data.stream()
|
|
@@ -82,6 +90,7 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
|
|
|
|
|
|
@Override
|
|
|
public PK insert(E entity) {
|
|
|
+ if (entity.getId() == null) entity.setId(getIDGenerator().generate());
|
|
|
tryValidate(entity);
|
|
|
getDao().insert(entity);
|
|
|
return entity.getId();
|