From 9ec7fc7d6a4516694889b32de5d98397d9125d4c Mon Sep 17 00:00:00 2001 From: Robin Shen Date: Wed, 18 Sep 2024 12:35:41 +0800 Subject: [PATCH] fix: Scrolling a Column in Board View crashes when fetching further Issues from Server (OD-2078) --- .../impl/DefaultBuildManager.java | 4 +--- .../impl/DefaultCodeCommentManager.java | 1 + .../impl/DefaultIssueManager.java | 2 ++ .../impl/DefaultProjectManager.java | 2 ++ .../impl/DefaultPullRequestManager.java | 2 +- .../persistence/dao/BaseEntityManager.java | 18 ++++++++++++++++++ .../component/issue/list/IssueListPanel.java | 2 +- 7 files changed, 26 insertions(+), 5 deletions(-) diff --git a/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultBuildManager.java b/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultBuildManager.java index 27ae82549e..8a3598c074 100644 --- a/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultBuildManager.java +++ b/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultBuildManager.java @@ -586,9 +586,7 @@ private void applyOrders(From root, CriteriaQuery criteriaQuery else orders.add(builder.desc(BuildQuery.getPath(root, Build.ORDER_FIELDS.get(sort.getField())))); } - - if (orders.isEmpty()) - orders.add(builder.desc(root.get(Build.PROP_ID))); + addOrderByIdIfNecessary(builder, root, orders); criteriaQuery.orderBy(orders); } diff --git a/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultCodeCommentManager.java b/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultCodeCommentManager.java index c2b3ee1a70..439a4465d6 100644 --- a/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultCodeCommentManager.java +++ b/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultCodeCommentManager.java @@ -261,6 +261,7 @@ private CriteriaQuery buildCriteriaQuery(Project project, Session s if (orders.isEmpty()) orders.add(builder.desc(CodeCommentQuery.getPath(root, CodeComment.PROP_LAST_ACTIVITY + "." + LastActivity.PROP_DATE))); + addOrderByIdIfNecessary(builder, root, orders); query.orderBy(orders); return query; diff --git a/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultIssueManager.java b/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultIssueManager.java index e5a510f330..3e2632dc6e 100644 --- a/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultIssueManager.java +++ b/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultIssueManager.java @@ -237,6 +237,8 @@ public List buildOrders(List sorts if (orders.isEmpty()) orders.add(builder.desc(IssueQuery.getPath(issue, Issue.PROP_LAST_ACTIVITY + "." + LastActivity.PROP_DATE))); + addOrderByIdIfNecessary(builder, issue, orders); + return orders; } diff --git a/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultProjectManager.java b/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultProjectManager.java index 9f58163fa6..79e901e8bb 100644 --- a/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultProjectManager.java +++ b/server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultProjectManager.java @@ -955,6 +955,8 @@ private CriteriaQuery buildCriteriaQuery(Session session, EntityQuery

buildCriteriaQuery(Session session, @Nullable if (orders.isEmpty()) orders.add(builder.desc(PullRequestQuery.getPath(root, PullRequest.PROP_LAST_ACTIVITY + "." + LastActivity.PROP_DATE))); - + addOrderByIdIfNecessary(builder, root, orders); query.orderBy(orders); return query; diff --git a/server-core/src/main/java/io/onedev/server/persistence/dao/BaseEntityManager.java b/server-core/src/main/java/io/onedev/server/persistence/dao/BaseEntityManager.java index 9db946c177..f28326a940 100644 --- a/server-core/src/main/java/io/onedev/server/persistence/dao/BaseEntityManager.java +++ b/server-core/src/main/java/io/onedev/server/persistence/dao/BaseEntityManager.java @@ -6,6 +6,11 @@ import io.onedev.server.model.AbstractEntity; import io.onedev.server.util.ReflectionUtils; +import org.hibernate.query.criteria.internal.path.SingularAttributePath; + +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.From; +import javax.persistence.criteria.Order; public abstract class BaseEntityManager implements EntityManager { @@ -93,4 +98,17 @@ public Class getEntityClass() { return entityClass; } + protected void addOrderByIdIfNecessary(CriteriaBuilder builder, From from, List orders) { + boolean orderByIdFound = false; + for (var order: orders) { + if (order.getExpression() instanceof SingularAttributePath + && ((SingularAttributePath) order.getExpression()).getAttribute().getName().equals(AbstractEntity.PROP_ID)) { + orderByIdFound = true; + break; + } + } + if (!orderByIdFound) + orders.add(builder.desc(from.get(AbstractEntity.PROP_ID))); + } + } diff --git a/server-core/src/main/java/io/onedev/server/web/component/issue/list/IssueListPanel.java b/server-core/src/main/java/io/onedev/server/web/component/issue/list/IssueListPanel.java index 2eae75377a..ca58fd211d 100644 --- a/server-core/src/main/java/io/onedev/server/web/component/issue/list/IssueListPanel.java +++ b/server-core/src/main/java/io/onedev/server/web/component/issue/list/IssueListPanel.java @@ -1503,7 +1503,7 @@ public Iterator iterator(long first, long count) { try { var query = queryModel.getObject(); if (query != null) { - return getIssueManager().query(getProjectScope(), (IssueQuery) query, + return getIssueManager().query(getProjectScope(), query, true, (int) first, (int) count).iterator(); } } catch (ExplicitException e) {