ref[IEntity]: delete empty() method and EntityCache load() method can return null entity

This commit is contained in:
godotg
2024-07-01 14:06:54 +08:00
parent c475a6ec4a
commit 688c2a9917
3 changed files with 3 additions and 22 deletions
+1 -2
View File
@@ -151,8 +151,7 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
// 如果数据库中不存在则给一个默认值
if (entity == null) {
// 数据库无法加载缓存,返回默认值
logger.warn("[{}] can not load [pk:{}] and use default entity to replace it", entityDef.getClazz().getSimpleName(), pk);
entity = (E) entityDef.newEmptyEntity();
logger.warn("[{}] can not load [pk:{}] and use null to replace it", entityDef.getClazz().getSimpleName(), pk);
}
pnode = new PNode<>(entity);
cache.put(pk, pnode);
@@ -13,6 +13,7 @@
package com.zfoo.orm.cache;
import com.zfoo.orm.model.IEntity;
import org.springframework.lang.Nullable;
import java.util.List;
import java.util.function.BiConsumer;
@@ -28,6 +29,7 @@ public interface IEntityCache<PK extends Comparable<PK>, E extends IEntity<PK>>
* <p>
* CN: 从数据库中加载数据到缓存,如果数据库不存在则返回一个id为空的默认值。可以通过 entity.empty() 方法判断id是否为空
*/
@Nullable
E load(PK pk);
/**
@@ -12,8 +12,6 @@
package com.zfoo.orm.model;
import com.zfoo.protocol.util.StringUtils;
/**
* @author godotg
*/
@@ -24,22 +22,4 @@ public interface IEntity<PK extends Comparable<PK>> {
*/
PK id();
/**
* 判空:由于查询不存在时缓存中也会有一份,因此判断为空需要根据实际类型才能决定
*
* @return EntityCaches中取出的值在数据库中是否存在
*/
default boolean empty() {
PK idValue = id();
if (idValue == null) {
return true;
}
// id在启动的时候做过校验,只能是int long float double String ObjectId几种类型
if (idValue instanceof Number) {
return ((Number) idValue).doubleValue() == 0D;
} else {
return StringUtils.isEmpty(idValue.toString());
}
}
}