Hi,
I did a little test. My data access class is here, but you can understand how it works
Master:
@NamedQuery(name = "product", query = "select t from productTest t order by t.name")
@Entity(name="productTest")
@Table(name = "productTest")
@Getter @Setter
public class ProductTest extends Model {
private static final long serialVersionUID = 7412735202355089750L;
@Column(name="name",length = 50, nullable = false)
private String name = "";
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<DepartmentTest> departament = new HashSet<>();
}
Detail:
@NamedQuery(name = "department", query = "select t from departmentTest t order by t.name")
@Entity(name="departmentTest")
@Table(name = "departmentTest")
@Getter @Setter
public class DepartmentTest extends Model {
private static final long serialVersionUID = 2798505907519151322L;
@Column(name="name",length = 50, nullable = false)
private String name = "";
}
Data:
insert into `departmenttest`(`id`,`name`,`tz1user`) values (1,'Test1',0),(2,'Test2',0),(3,'Test3',0);
insert into `producttest`(`id`,`name`,`tz1user`) values (1,'Product 01',0);
//Hibernate table join
insert into `producttest_departmenttest`(`productTest_id`,`departament_id`) values (1,2);
Class:
public class DepartmentMultiSelectListBox extends MultiSelectListBox<DepartmentTest> {
private static final long serialVersionUID = -5569052352237294032L;
private Tz1Controller<DepartmentTest> departmenTz1Controller = new Tz1Controller<>(DepartmentTest.class);
public DepartmentMultiSelectListBox() {
setItems(departmenTz1Controller.setQueryNamed("department").getResult());
setRenderer(new ComponentRenderer<>(column -> new Tz1Label(column.getName())));
}
Main:
@Route("test")
public class MultiSelectTest extends VerticalLayout {
private static final long serialVersionUID = 1320561879546625026L;
private DepartmentMultiSelectListBox departmentMultiSelectListBox = new DepartmentMultiSelectListBox();
private Tz1Controller<ProductTest> productTz1Controller = new Tz1Controller<>(ProductTest.class);
private Button testButton = new Button("test me");
public MultiSelectTest() {
testButton.addClickListener(e -> testButtonClick());
add(departmentMultiSelectListBox,testButton);
}
private void testButtonClick() {
departmentMultiSelectListBox.setValue(productTz1Controller.setQueryNamed("product").getResult().get(0).getDepartament());
}
}
If click in testButton, error:
08:20:56,486 ERROR [com.vaadin.flow.server.DefaultErrorHandler]
(default task-2) : java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at deployment.proativo-1.0.war//com.vaadin.flow.component.listbox.MultiSelectListBox.lambda$presentationToModel$1(MultiSelectListBox.java:61)
It only works if SetValue contains the same objects as SetItems, but this is not how combox, listbox, etc … Try binder, it doesn’t work either.