Skip to content

Commit

Permalink
applied stack overflow feedback #https://stackoverflow.com/questions/…
Browse files Browse the repository at this point in the history
…77943523 and the @onetomany and @manytoone now compiles. realizing now that even my Ape @OnetoOne example wasn't binding correctly in the grid view, I've made several attempts and it now binds to read the value, but not to save the value.  Also, favoriteBatColor does not save the value from the ComboBox, and I would like to combo box to populate from the colors of existing bats, I suspect that I will need to add logic to the Service but even the SamplePersonService does not have a clear path to all bats, so for now i have a hard coded set of colors that do show in the combobox
  • Loading branch information
sketchbook22 committed Feb 6, 2024
1 parent c67c244 commit 32e01f8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/example/application/data/Bat.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class Bat extends AbstractEntity {

private String color;

@JsonBackReference
@ManyToOne
@JoinColumn(name = "zookeeper")
private SamplePerson zooKeeper;
public String getColor() {
return color;
Expand All @@ -25,9 +28,7 @@ public void setColor(String value) {
this.color = value;
}

@JsonBackReference
@ManyToOne
@JoinColumn(name = "SAMPlE_PERSON_ID", referencedColumnName = "SAMPLE_PERSON_ID", nullable = false)

public SamplePerson getZooKeeper() {
return zooKeeper;
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/example/application/data/SamplePerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Entity
Expand All @@ -13,8 +15,11 @@ public class SamplePerson extends AbstractEntity {
@JoinColumn(name="ape")
private Ape ape;


@JsonManagedReference
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "zooKeeper")
private Set<Bat> bats;

private String favoriteBatColor;
private String c;
private String d;
private String e;
Expand All @@ -26,14 +31,19 @@ public void setApe(Ape value) {
this.ape = value;
}

@JsonManagedReference
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "zooKeeper")

public Set<Bat> getBats() {
return bats;
}
public void setBats(Set<Bat> value) {
this.bats = value;
}
public String getFavoriteBatColor(){
return favoriteBatColor;
}
public void setFavoriteBatColor(String value){
this.favoriteBatColor = value;
}
public String getC() {
return c;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.example.application.data.SamplePerson;
import com.example.application.data.SamplePersonRepository;

import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.dependency.Uses;
import com.vaadin.flow.component.formlayout.FormLayout;
Expand Down Expand Up @@ -42,8 +43,8 @@ public class MasterDetailView extends Div implements BeforeEnterObserver {

private final Grid<SamplePerson> grid = new Grid<>(SamplePerson.class, false);

private TextField apeColor;
private TextField batColors;
private TextField ape_color;
private ComboBox<String> batColors;
private TextField c;
private TextField d;
private TextField e;
Expand Down Expand Up @@ -71,7 +72,7 @@ public MasterDetailView(SamplePersonService samplePersonService) {

// Configure Grid
grid.addColumn("ape.color").setHeader("Ape Color").setAutoWidth(true);
grid.addColumn("b").setAutoWidth(true);
grid.addColumn("favoriteBatColor").setAutoWidth(true);
grid.addColumn("c").setAutoWidth(true);
grid.addColumn("d").setAutoWidth(true);
grid.addColumn("e").setAutoWidth(true);
Expand All @@ -95,6 +96,10 @@ public MasterDetailView(SamplePersonService samplePersonService) {
// Configure Form
binder = new BeanValidationBinder<>(SamplePerson.class);

binder.bind(ape_color,
samplePerson -> samplePerson.getApe().getColor(),
(samplePerson, value) -> samplePerson.getApe().setColor(value));

// Bind fields. This is where you'd define e.g. validation rules

binder.bindInstanceFields(this);
Expand Down Expand Up @@ -154,12 +159,14 @@ private void createEditorLayout(SplitLayout splitLayout) {
editorLayoutDiv.add(editorDiv);

FormLayout formLayout = new FormLayout();
apeColor = new TextField("Ape Color");
batColors = new TextField("Bat Colors");
ape_color = new TextField("Ape Color");
batColors = new ComboBox<>("Bat Colors");
c = new TextField("c");
d = new TextField("d");
e = new TextField("e");
formLayout.add(apeColor, batColors, c, d, e);
batColors.setItems("black","brown","white","blue","red");

formLayout.add(ape_color, batColors, c, d, e);

editorDiv.add(formLayout);
createButtonLayout(editorLayoutDiv);
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
insert into ape(version, id, color) values (1, 1, 'apricot')
insert into sample_person(version, id, ape, c, d, e) values (1, 1, 1, 'c', 'd', 'e')
insert into bat(version, id, color, zooKeeper) values (1, 1, 'brown', 1)
insert into bat(version, id, color, zooKeeper) values (1, 1, 'blue', 1)
insert into sample_person(version, id, ape, bats, c, d, e) values (1, 1, 1, ARRAY [1, 2], 'c', 'd', 'e')
insert into bat(version, id, color, zooKeeper) values (1, 2, 'blue', 1)

0 comments on commit 32e01f8

Please sign in to comment.