I'm working with thymeleaf on a small project. And have a setup like so.
public class Datacenter {
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY, orphanRemoval = true, mappedBy="datacenter"
private Set<Cage> cageSet = new HashSet<>();
}
public class Cage {
@ManyToOne
@JoinColumn(name = "datacenter_id")
private Datacenter datacenter;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY, orphanRemoval = true, mappedBy="rack"
private Set<Rack> rackSet= new HashSet<>();
}
public class Rack {
@ManyToOne
@JoinColumn(name = "cage_id")
private Cage cage;
private String var1;
private String var2;
private Boolean active;
}
Thing is that I want to have a page that has a collapsible list of datacenters and when it expands in the next row to a table that lists all the Cages defined there. Next below that I would like to have a row that shows values from one certain object Rack from the related Set<Rack>
- more precisely only one would have the active value as true
.
Is there an option through Thymeleaf to reference that one object using something like:
th:text="${cage.rackSet.stream().filter(some_filtering_here).getVar2()}"
Or should I create a sepparate object hierarchy on the controller level that would mimic exactly how I want the page to look like?
For these kinds of things, look at collection selection and collection projection. In your case an expression like this should work:
th:text="${cage.rackSet.^[active].var2}"