Thymeleaf lists.sort causes LazyInitializationException despite open session in view

225 Views Asked by At

I have a Sprint Boot project with open session in view. When I try to sort a list like this

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

I get a LazyInitializationException

1

There are 1 best solutions below

0
On

Reason is that Thymeleaf

  • creates a copy of the list via constructor.
  • the type of the list is a org.hibernate.collection.internal.PersistentBag
  • adds the sorted elements in the copy of the list
  • PersistentBag calls initialze()

Workaround: Wrap the list in a normal list

<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>