0

我有一个 Sprint Boot 项目,其中包含开放会话。当我尝试对这样的列表进行排序时

<th:block th:each="e : ${#lists.sort(entity.getElements(), new com.example.CustomerComparator())}"
  th:include="fragments/table-row::row(elem = ${e})">
</th:block>

我得到一个 LazyInitializationException

4

1 回答 1

0

原因是百里香叶

  • 通过构造函数创建列表的副本。
  • 列表的类型是 org.hibernate.collection.internal.PersistentBag
  • 在列表的副本中添加已排序的元素
  • PersistentBag 调用 initialze()

解决方法:将列表包装在普通列表中

<th:block th:each="e : ${#lists.sort(new java.util.ArrayList(entity.getElements()), new com.example.CustomerComparator())}"
  th:include="fragments/table-row::row(elem = ${e})">
</th:block>
于 2021-02-01T13:42:20.067 回答