Hello,
I’ve been searching for days but can’t find how to bind two entities with many to many relationship (spring boot, hibernate, vaadin 10). Every example I’ve found is about text fields.
I don’t know how to bind with getters and setters or with other way. Can anyone help me, please?
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinTable(name="module_project", joinColumns={@JoinColumn(referencedColumnName="ID")}
, inverseJoinColumns={@JoinColumn(referencedColumnName="ID")})
private List<Module> modules;
@Entity
public class Module {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "modules")
@Fetch(value = FetchMode.SUBSELECT)
private List<Project> projects;
@SpringComponent
@Scope("request")
public class ProjectEditorDialog extends AbstractEditorDialog<Project> {
private ListBox<Module> lbLeft;
private Button bLeft;
private Button bRight;
private ListBox<Module> lbRight;
private Module tempCabecera;
private List<Module> listLeftCabeceras = new ArrayList<Module>();
private List<Module> listRightCabeceras = new ArrayList<Module>();
---.....-----
private void createLeftColumn() {
VerticalLayout leftColumn = new VerticalLayout();
Label lTitle = new Label("Dimensiones disponibles");
leftColumn.add(lTitle);
lbLeft = new ListBox<Module>();
listLeftCabeceras = moduleRepository.findByUrl("proyectos");
lbLeft.setItems(listLeftCabeceras);
lbLeft.setRenderer(new ComponentRenderer<>(item -> {
Label label = new Label(item.getCabecera().getName());
label.getStyle().set("propertyName", "value");
return label;
}));
leftColumn.add(lbLeft);
getHorizontalLayout().add(leftColumn);
}
private void createCenterColumn() {
VerticalLayout centerColumn = new VerticalLayout();
bRight = new Button(">>");
bRight.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
private static final long serialVersionUID = -7189656556900852946L;
@Override
public void onComponentEvent(ClickEvent<Button> event) {
tempCabecera = lbLeft.getValue();
if (tempCabecera != null) {
listLeftCabeceras.remove(tempCabecera);
listRightCabeceras.add(tempCabecera);
Collections.sort(listLeftCabeceras, new Comparator<Module>() {
@Override
public int compare(Module lCabecera, Module rCabecera) {
return lCabecera.getName().compareTo(rCabecera.getName());
}
});
lbLeft.setItems(listLeftCabeceras);
lbRight.setItems(listRightCabeceras);
checkButtons();
}
}
});
bLeft = new Button("<<");
bLeft.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
private static final long serialVersionUID = -7189656556900852946L;
@Override
public void onComponentEvent(ClickEvent<Button> event) {
tempCabecera = lbRight.getValue();
if (tempCabecera != null) {
listRightCabeceras.remove(tempCabecera);
listLeftCabeceras.add(tempCabecera);
lbLeft.setItems(listLeftCabeceras);
lbRight.setItems(listRightCabeceras);
checkButtons();
}
}
});
centerColumn.add(bRight);
centerColumn.add(bLeft);
getHorizontalLayout().add(centerColumn);
}
private void createRightColumn() {
VerticalLayout rightColumn = new VerticalLayout();
Label lTitle = new Label("Dimensiones seleccionadas");
rightColumn.add(lTitle);
lbRight = new ListBox<Module>();
lbRight.setRenderer(new ComponentRenderer<>(item -> {
Label label = new Label(item.getCabecera().getName());
label.getStyle().set("propertyName", "value");
return label;
}));
rightColumn.add(lbRight);
getHorizontalLayout().add(rightColumn);
***** I think here is the place where I have to bind the result, but is not working. This is several test I did *******
//getBinder().forField(aux).bind(Project::getBudget, Project::setBudget);
//getBinder().bindInstanceFields(listRightCabeceras);
//getBinder().getBinding("Project.modules");
//getBinder().forField(lbRight).bind("Project.modules");
//getBinder().bindInstanceFields(getBinder().getBean());
//getBinder().forField(lbRight).bind(List.class);
***** ... *******
}
private void checkButtons() {
if (listRightCabeceras.size() > 0)
bLeft.setEnabled(true);
else
bLeft.setEnabled(false);
if (listLeftCabeceras.size() > 0)
bRight.setEnabled(true);
else
bRight.setEnabled(false);
}
---.................---