Skip to content

Commit

Permalink
修改一对多子表新建时使用已有实体
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 3, 2025
1 parent 7bdb95d commit 629eaf1
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 5 deletions.
6 changes: 4 additions & 2 deletions nop-biz/src/main/java/io/nop/biz/crud/OrmEntityCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ void syncEntitySet(Collection<?> c, IOrmEntitySet<IOrmEntity> refSet, String key
} else {
if (chgType == null || chgType.contains(DaoConstants.CHANGE_TYPE_UPDATE)) {
IOrmEntity refEntity = dao.loadEntityById(id);
checkRefEntity(refEntity, item, owner, refModel);
// 不检查owner
checkRefEntity(refEntity, item, null, refModel);
copyToEntity(item, refEntity, field, objMeta, baseBizObjName, BizConstants.METHOD_UPDATE, scope);
ret.add(refEntity);
}
Expand Down Expand Up @@ -422,7 +423,8 @@ private void assignRefProps(Object item, IOrmEntity owner, IEntityRelationModel
String rightProp = join.getRightProp();
if (rightProp != null) {
Object leftValue = OrmEntityHelper.getLeftValue(join, owner);
BeanTool.instance().setProperty(item, rightProp, leftValue);
if (leftValue != null)
BeanTool.instance().setProperty(item, rightProp, leftValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MODULE_ID,MODULE_NAME,MODULE_VERSION,DISPLAY_NAME,BASE_MODULE_ID,BASE_PACKAGE_NAME,ENTITY_PACKAGE_NAME,MAVEN_GROUP_ID,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROP_META_ID,ENTITY_META_ID,IS_MANDATORY,PROP_NAME,DISPLAY_NAME,STD_SQL_TYPE,PRECISION,SCALE,PROP_ID,UI_SHOW,UI_CONTROL,DOMAIN_ID,STD_DOMAIN_NAME,DICT_NAME,DYN_PROP_MAPPING,TAGS_TEXT,DEFAULT_VALUE,EXT_CONFIG,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SEQ_NAME,SEQ_TYPE,IS_UUID,NEXT_VALUE,STEP_SIZE,CACHE_SIZE,MAX_VALUE,RESET_TYPE,DEL_FLAG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_chgType,ENTITY_META_ID,MODULE_ID,ENTITY_NAME,DISPLAY_NAME,TABLE_NAME,QUERY_SPACE,STORE_TYPE,TAGS_TEXT,IS_EXTERNAL,STATUS,EXT_CONFIG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,@var:NopDynEntityMeta@entityMetaId,@var:NopDynModule@moduleId,UserEntity,User Entity,,,10,,false,1,,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynEntityMeta@entityMetaId_1,@var:NopDynModule@moduleId,RoleEntity,Role Entity,,,10,,false,1,,0,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_chgType,REL_META_ID,ENTITY_META_ID,REF_ENTITY_META_ID,RELATION_NAME,RELATION_DISPLAY_NAME,RELATION_TYPE,MIDDLE_TABLE_NAME,MIDDLE_ENTITY_NAME,LEFT_PROP_NAME,RIGHT_PROP_NAME,REF_SET_KEY_PROP,REF_SET_SORT,STATUS,TAGS_TEXT,EXT_CONFIG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,@var:NopDynEntityRelationMeta@relMetaId,@var:NopDynEntityMeta@entityMetaId_1,@var:NopDynEntityMeta@entityMetaId,roleUsers,测试一对多关联,o2m,,,sid,roleId,,,1,"pub,insertable",,0,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_chgType,MODULE_ID,MODULE_NAME,MODULE_VERSION,DISPLAY_NAME,BASE_MODULE_ID,BASE_PACKAGE_NAME,ENTITY_PACKAGE_NAME,MAVEN_GROUP_ID,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME
A,@var:NopDynModule@moduleId,relation-demo,1,Demo Module,,,,,10,0,autotest-ref,*,autotest-ref,*
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_chgType,PROP_META_ID,ENTITY_META_ID,IS_MANDATORY,PROP_NAME,DISPLAY_NAME,STD_SQL_TYPE,PRECISION,SCALE,PROP_ID,UI_SHOW,UI_CONTROL,DOMAIN_ID,STD_DOMAIN_NAME,DICT_NAME,DYN_PROP_MAPPING,TAGS_TEXT,DEFAULT_VALUE,EXT_CONFIG,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,@var:NopDynPropMeta@propMetaId_1,@var:NopDynEntityMeta@entityMetaId,true,userAge,userAge Display,INTEGER,0,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynPropMeta@propMetaId_3,@var:NopDynEntityMeta@entityMetaId_1,true,roleName,roleName Display,VARCHAR,100,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynPropMeta@propMetaId,@var:NopDynEntityMeta@entityMetaId,true,userName,userName Display,VARCHAR,100,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynPropMeta@propMetaId_4,@var:NopDynEntityMeta@entityMetaId_1,true,roleKey,roleKey Display,VARCHAR,100,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynPropMeta@propMetaId_2,@var:NopDynEntityMeta@entityMetaId,true,roleId,roleId Display,VARCHAR,100,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_chgType,SEQ_NAME,SEQ_TYPE,IS_UUID,NEXT_VALUE,STEP_SIZE,CACHE_SIZE,MAX_VALUE,RESET_TYPE,DEL_FLAG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,default,seq,0,1,1,100,,,0,0,autotest-ref,*,autotest-ref,*,
18 changes: 15 additions & 3 deletions nop-orm/src/main/java/io/nop/orm/session/CascadeFlusher.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ void _cascadeEntity(IOrmEntity entity, boolean autoCascadeDelete) {
return;



// 对于proxy的情况,不需要处理
if (state.isProxy()) {
if (entity.orm_extDirty()) {
Expand Down Expand Up @@ -361,8 +360,9 @@ private boolean isOrphan(IOrmEntity entity, IOrmEntitySet coll) {
*/
private void addOwnerJoinProps(IOrmEntitySet<IOrmEntity> coll, IEntityRelationModel relModel) {
IOrmEntity owner = coll.orm_owner();
for (IOrmEntity entity : coll) {
if (entity.orm_state().isTransient()) {
if (owner.orm_state().isUnsaved()) {
// 如果是新建实体,则它的主键可能是临时分配的,需要同步到子表记录上
for (IOrmEntity entity : coll) {
for (IEntityJoinConditionModel join : relModel.getJoin()) {
IEntityPropModel rightProp = join.getRightPropModel();
if (rightProp != null) {
Expand All @@ -371,6 +371,18 @@ private void addOwnerJoinProps(IOrmEntitySet<IOrmEntity> coll, IEntityRelationMo
}
}
}
} else {
for (IOrmEntity entity : coll) {
if (entity.orm_state().isTransient()) {
for (IEntityJoinConditionModel join : relModel.getJoin()) {
IEntityPropModel rightProp = join.getRightPropModel();
if (rightProp != null) {
Object leftValue = OrmEntityHelper.getLeftValue(join, owner);
OrmEntityHelper.setPropValue(rightProp, entity, leftValue);
}
}
}
}
}
}
}

0 comments on commit 629eaf1

Please sign in to comment.