mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-12 20:16:07 +00:00
feat[IEntityWrapper]: new entity method
This commit is contained in:
1 parent
e13cdd424a
commit
304a37bebe
6 files changed
+17
-30
No files matched your search
+1
-1
@@ -164,7 +164,7 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
|
||||
|
||||
// 如果数据库中不存在则给一个默认值
|
||||
if (entity == null) {
|
||||
entity = (E) entityDef.newEntity(pk);
|
||||
entity = (E) wrapper.newEntity(pk);
|
||||
OrmContext.getAccessor().insert(entity);
|
||||
}
|
||||
pnode = new PNode<>(entity);
|
||||
|
||||
@@ -68,6 +68,7 @@ public abstract class EnhanceUtils {
|
||||
Class<?> clazz = entityWrapper.getEntityClass();
|
||||
Field idField = entityWrapper.getIdField();
|
||||
Method setIdMethod = entityWrapper.getSetIdMethod();
|
||||
Field versionField = entityWrapper.getVersionField();
|
||||
Method getVersionMethod = entityWrapper.getGetVersionMethod();
|
||||
Method setVersionMethod = entityWrapper.getSetVersionMethod();
|
||||
|
||||
@@ -83,11 +84,11 @@ public abstract class EnhanceUtils {
|
||||
enhanceClazz.addMethod(newEntityMethod);
|
||||
|
||||
// 定义类实现的接口方法name
|
||||
CtMethod nameMethod = new CtMethod(classPool.get(String.class.getName()), "versionFieldName", null, enhanceClazz);
|
||||
nameMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
|
||||
String nameMethodBody = StringUtils.format("{ return \"{}\"; }", entityWrapper.versionFieldName());
|
||||
nameMethod.setBody(nameMethodBody);
|
||||
enhanceClazz.addMethod(nameMethod);
|
||||
CtMethod versionFieldNameMethod = new CtMethod(classPool.get(String.class.getName()), "versionFieldName", null, enhanceClazz);
|
||||
versionFieldNameMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
|
||||
String versionFieldNameMethodBody = versionField == null ? "{ return null; }" : StringUtils.format("{ return \"{}\"; }", entityWrapper.versionFieldName());
|
||||
versionFieldNameMethod.setBody(versionFieldNameMethodBody);
|
||||
enhanceClazz.addMethod(versionFieldNameMethod);
|
||||
|
||||
// 定义类实现的接口方法gvs
|
||||
CtMethod gvsMethod = new CtMethod(classPool.get(long.class.getName()), "gvs", classPool.get(new String[]{IEntity.class.getName()}), enhanceClazz);
|
||||
|
||||
@@ -29,8 +29,9 @@ public class EntityWrapper implements IEntityWrapper {
|
||||
|
||||
private Class<? extends IEntity<?>> entityClass;
|
||||
private Field idField;
|
||||
private Field versionField;
|
||||
private Method setIdMethod;
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
private Field versionField;
|
||||
private Method getVersionMethod;
|
||||
private Method setVersionMethod;
|
||||
|
||||
@@ -61,7 +62,10 @@ public class EntityWrapper implements IEntityWrapper {
|
||||
|
||||
@Override
|
||||
public String versionFieldName() {
|
||||
return idField.getName();
|
||||
if (versionField == null) {
|
||||
return null;
|
||||
}
|
||||
return versionField.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -396,7 +396,7 @@ public class OrmManager implements IOrmManager {
|
||||
indexTextDefMap.put(field.getName(), indexTextDef);
|
||||
}
|
||||
|
||||
return EntityDef.valueOf(idField, clazz, !hasUnsafeCollection, cacheStrategy.getSize(), cacheStrategy.getExpireMillisecond(), persisterStrategy, indexDefMap, indexTextDefMap);
|
||||
return EntityDef.valueOf(clazz, !hasUnsafeCollection, cacheStrategy.getSize(), cacheStrategy.getExpireMillisecond(), persisterStrategy, indexDefMap, indexTextDefMap);
|
||||
}
|
||||
|
||||
private void checkEntity(Class<?> clazz) {
|
||||
|
||||
@@ -12,11 +12,8 @@
|
||||
|
||||
package com.zfoo.orm.model;
|
||||
|
||||
import com.zfoo.orm.anno.Id;
|
||||
import com.zfoo.orm.config.PersisterStrategy;
|
||||
import com.zfoo.protocol.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -24,8 +21,6 @@ import java.util.Map;
|
||||
*/
|
||||
public class EntityDef {
|
||||
|
||||
private Field idField;
|
||||
|
||||
private Class<? extends IEntity<?>> clazz;
|
||||
|
||||
// 线程安全指的是内部没有使用集合或者使用的集合全部支持并发操作
|
||||
@@ -42,10 +37,9 @@ public class EntityDef {
|
||||
private Map<String, IndexTextDef> indexTextDefMap;
|
||||
|
||||
|
||||
public static EntityDef valueOf(Field idField, Class<? extends IEntity<?>> clazz, boolean threadSafe, int cacheSize, long expireMillisecond
|
||||
public static EntityDef valueOf(Class<? extends IEntity<?>> clazz, boolean threadSafe, int cacheSize, long expireMillisecond
|
||||
, PersisterStrategy persisterStrategy, Map<String, IndexDef> indexDefMap, Map<String, IndexTextDef> indexTextDefMap) {
|
||||
var entityDef = new EntityDef();
|
||||
entityDef.idField = idField;
|
||||
entityDef.clazz = clazz;
|
||||
entityDef.threadSafe = threadSafe;
|
||||
entityDef.cacheSize = cacheSize;
|
||||
@@ -56,18 +50,6 @@ public class EntityDef {
|
||||
return entityDef;
|
||||
}
|
||||
|
||||
public IEntity<?> newEmptyEntity() {
|
||||
var entity = ReflectionUtils.newInstance(clazz);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public IEntity<?> newEntity(Object id) {
|
||||
var entity = ReflectionUtils.newInstance(clazz);
|
||||
var idFields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Id.class);
|
||||
ReflectionUtils.makeAccessible(idFields[0]);
|
||||
ReflectionUtils.setField(idFields[0], entity, id);
|
||||
return entity;
|
||||
}
|
||||
public Class<? extends IEntity<?>> getClazz() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
package com.zfoo.orm.cache;
|
||||
|
||||
import com.mongodb.client.model.Filters;
|
||||
import com.zfoo.event.manager.EventBus;
|
||||
import com.zfoo.orm.OrmContext;
|
||||
import com.zfoo.orm.entity.MailEntity;
|
||||
import com.zfoo.orm.entity.UserEntity;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.protocol.util.ThreadUtils;
|
||||
import com.zfoo.scheduler.util.TimeUtils;
|
||||
import org.bson.Document;
|
||||
@@ -53,7 +53,7 @@ public class EntityCachesTest {
|
||||
for (var i = 1; i <= 10; i++) {
|
||||
for (var j = 1; j <= 10; j++) {
|
||||
var entity = userEntityCaches.load((long) j);
|
||||
entity.setE("update" + j);
|
||||
entity.setE(StringUtils.format("update-{}-{}", i, j));
|
||||
entity.setC(j);
|
||||
userEntityCaches.update(entity);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user