hs-web/hsweb-framework · error · ValidationException
parentId
parentId
Error message
子节点ID不能与父节点ID相同
What it means
TreeSupportEntity.tryValidate() adds a tree-specific rule on top of standard bean validation: a node's id must never equal its parentId, which would create a self-referencing cycle. It throws ValidationException with field 'parentId' and message '子节点ID不能与父节点ID相同' (child ID must not equal parent ID).
Solutions
- Ensure parentId is null for root nodes instead of copying the node's own id.
- Fix the client/form so parentId only contains a real, distinct parent identifier.
- Before saving, validate that id != parentId and reject or correct the payload.
- For re-parenting, verify the target parent is not the node itself (or a descendant) to avoid cycles.
Example fix
// before
{"id":"100","parentId":"100","name":"dept"} // self-parent
// after
{"id":"100","parentId":null,"name":"dept"} // root node Defensive patterns
Strategy: validation
Validate before calling
if (entity.getId() != null && entity.getId().equals(entity.getParentId())) {
throw new ValidationException("parentId", "子节点ID不能与父节点ID相同");
} Try / catch
try {
crudService.save(entity);
} catch (ValidationException e) {
if ("parentId".equals(e.getField())) {
// clear parentId (treat as root) or reject, then retry once
entity.setParentId(null);
crudService.save(entity);
} else {
throw e;
}
} Prevention
- Treat parentId=null as the convention for root nodes in clients and imports.
- Validate id != parentId (and no descendant cycles) before calling save/update.
- Sanitize batch imports so the parent column cannot carry the row's own id.
- Add form-level checks so the UI never copies id into parentId.
When it happens
Trigger: Saving or updating a tree entity (e.g. via CRUD save/update endpoints) where entity.getId() is not null and equals entity.getParentId().
Common situations: Client sends the same value in both id and parentId fields; frontend form copies id into parentId for root nodes; batch import where the parent column is misaligned; an update that re-parents a node to itself.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- error.page_size_exceeded
- 不支持的验证规则:
- 不支持的授权请求:
- unsupported dialect :
- join class [" + clazz + "] not found!
AI-assisted analysis of hs-web/hsweb-framework@b2cfc85a57 (2026-09-13).
Data as JSON: /api/errors/8e3a389c25c71f93.
Report an issue: GitHub.
Appendix: source
Thrown at hsweb-commons/hsweb-commons-api/src/main/java/org/hswebframework/web/api/crud/entity/TreeSupportEntity.java:114
*
* @return 节点层级
*/
void setLevel(Integer level);
/**
* 获取所有子节点,默认情况下此字段只会返回null.可以使用{@link TreeSupportEntity#list2tree(Collection, BiConsumer)}将
* 列表结构转为树形结构
*
* @param <T> 当前实体类型
* @return 自己节点
*/
<T extends TreeSupportEntity<PK>> List<T> getChildren();
@Override
default void tryValidate(Class<?>... groups) {
Entity.super.tryValidate(groups);
if (getId() != null && Objects.equals(getId(), getParentId())) {
throw new ValidationException("parentId", "子节点ID不能与父节点ID相同");
}
}
/**
* 根据path获取父节点的path
*
* @param path path
* @return 父节点path
*/
static String getParentPath(String path) {
if (path == null || path.length() < 4) {
return null;
}
return path.substring(0, path.length() - 5);
}
static <T extends TreeSupportEntity> void forEach(Collection<T> list, Consumer<T> consumer) {
Queue<T> queue = new LinkedList<>(list);View on GitHub (pinned to b2cfc85a57)