Browse Source

优化id判断

zhou-hao 6 years ago
parent
commit
392c495792

+ 3 - 3
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/AbstractTreeSortService.java

@@ -119,7 +119,7 @@ public abstract class AbstractTreeSortService<E extends TreeSortSupportEntity<PK
 
     @Override
     public PK insert(E entity) {
-        if (entity.getId() == null) {
+        if (StringUtils.isEmpty(entity.getId())) {
             entity.setId(getIDGenerator().generate());
         }
         applyPath(entity);
@@ -156,8 +156,8 @@ public abstract class AbstractTreeSortService<E extends TreeSortSupportEntity<PK
     protected PK saveOrUpdateForSingle(E entity) {
         assertNotNull(entity);
         PK id = entity.getId();
-        if (null == id || this.selectByPk(id) == null) {
-            if (null == id) {
+        if (StringUtils.isEmpty(id) || this.selectByPk(id) == null) {
+            if (StringUtils.isEmpty(id)) {
                 entity.setId(getIDGenerator().generate());
             }
             applyPath(entity);

+ 2 - 2
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java

@@ -123,13 +123,13 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
 
     @Override
     public PK insert(E entity) {
-        if (entity.getId() != null) {
+        if (!StringUtils.isEmpty(entity.getId())) {
             if ((entity.getId() instanceof String) && !StringUtils.isEmpty(entity.getId())) {
                 tryValidateProperty(entity.getId().toString().matches("[a-zA-Z0-9_\\-]+"), "id", "只能由数字,字母,下划线,和-组成");
             }
             tryValidateProperty(selectByPk(entity.getId()) == null, "id", entity.getId() + "已存在");
         }
-        if (entity.getId() == null && getIDGenerator() != null) {
+        if (StringUtils.isEmpty(entity.getId()) && getIDGenerator() != null) {
             entity.setId(getIDGenerator().generate());
         }
         if (entity instanceof RecordCreationEntity) {