Skip to content
Advertisement

How to get an average int for a Collection of an Entity using thymeleaf?

I have an Employee Entity that has a Collection of Reviews, each Review has a byte grade.

Unnecessary details are omitted for brevity

@Entity
public class Employee extends AbstractBaseEntity {
    @OneToMany( mappedBy = "employee",  cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
    private List<Review> reviews;
}
@Entity
public class Review extends AbstractBaseEntity {
    private Byte grade;
}

How can I get an average Review grade for each Employee using Thymeleaf to insert in Table?

Advertisement

Answer

Thymeleaf has aggregate functions. You can combine them with collection projection.

<span th:text="${#aggregates.avg(employee.reviews.![grade])}" />
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement