Search code examples
spring-bootthymeleaflinkedhashmap

Wrong Thymeleaf iteration


I dont know why but when i iterate a Map with thymeleaf, The index order is changing... ?

    <form method="POST" action="/deleteValue">
        <tr th:each="weight : ${user.weights}">
            <span th:text="${weightStat.index}">index</span>
            - <span th:text="${#dates.format(new java.util.Date(weight.key))}"></span>
            - <span th:text="${weight.value}">value</span>Kg
            <input type="hidden" name="key" th:value="${weight.key}"/>
            <button type="submit">X</button>
            <br>
        </tr>
    </form>

OUTPUT (Wrong order), H2 DB (Real order)


Solution

  • I found the issue, Hibernate change automatically Linked or Tree Map to classic hashmap... solution : change the getter

    public Map<Long, Double> getWeights() { return new TreeMap<>(this.weights); }