Skip to content

Commit

Permalink
fixed many to many mapping by using an Association Table, related to …
Browse files Browse the repository at this point in the history
…this SO question #https://stackoverflow.com/questions/77943523 . Not related to the SO question I also have bat colors and car colors show up as a CSV on the table, but so far have failed to populate the combo box with those values.
  • Loading branch information
sketchbook22 committed Feb 8, 2024
1 parent 4be15ce commit f31fea4
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 20 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/example/application/data/Car.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.application.data;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import org.hibernate.annotations.LazyCollection;
Expand All @@ -15,9 +16,9 @@ public class Car extends AbstractEntity {

private String color;

@JsonBackReference
@ManyToMany(mappedBy="cars")
private Set<SamplePerson> drivers;
@JsonManagedReference
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "car")
private Set<SamplePersonAssocCar> drivers;

public String getColor() {
return color;
Expand All @@ -26,8 +27,8 @@ public void setColor(String value) {
this.color = value;
}

public Set<SamplePerson> getDrivers() { return drivers;}
public void setDrivers(Set<SamplePerson> value){
public Set<SamplePersonAssocCar> getDrivers() { return drivers;}
public void setDrivers(Set<SamplePersonAssocCar> value){
this.drivers = value;
}

Expand Down
37 changes: 27 additions & 10 deletions src/main/java/com/example/application/data/SamplePerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ public class SamplePerson extends AbstractEntity {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "zooKeeper")
private Set<Bat> bats;

private String favoriteBatColor;
private String favoriteBatColor = "soup";

@JsonManagedReference
@ManyToMany
@JoinTable(
name = "CAR_DRIVER_ASSOC",
joinColumns = {@JoinColumn(name="CAR_ID")},
inverseJoinColumns = {@JoinColumn(name="ID")}
)
private Set<Car> cars;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "samplePerson")
private Set<SamplePersonAssocCar> cars;
private String d;
private String e;

Expand All @@ -45,16 +41,37 @@ public Set<Bat> getBats() {
public void setBats(Set<Bat> value) {
this.bats = value;
}

public List<String> getAllBatColors(){
List<String> re = new ArrayList<String>();
bats.forEach((bat) -> {
re.add(bat.getColor());
});
return re;
}
public String getAllBatColorsCsv(){
return getAllBatColors().toString();
}
public List<String> getAllCarColors(){
List<String> re = new ArrayList<String>();
cars.forEach((car) -> {
re.add(car.getCar().getColor());
});
return re;
}
public String getAllCarColorsCsv(){
return getAllCarColors().toString();
}
public String getFavoriteBatColor(){
return favoriteBatColor;
}
public void setFavoriteBatColor(String value){
this.favoriteBatColor = value;
}
public Set<Car> getCars() {
public Set<SamplePersonAssocCar> getCars() {
return cars;
}
public void setCars(Set<Car> value) {
public void setCars(Set<SamplePersonAssocCar> value) {
this.cars = value;
}
public String getD() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.application.data;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Set;

@Entity
public class SamplePersonAssocCar extends AbstractEntity {

@JsonBackReference
@ManyToOne
@JoinColumn(name = "samplePerson")
private SamplePerson samplePerson;

@JsonBackReference
@ManyToOne
@JoinColumn(name = "car")
private Car car;

public SamplePerson getSamplePerson() {
return samplePerson;
}
public void setSamplePerson(SamplePerson value) {
this.samplePerson = value;
}

public Car getCar() {
return car;
}
public void setCar(Car value) {
this.car = value;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.application.data;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface SamplePersonAssocCarRepository
extends
JpaRepository<SamplePersonAssocCar, Long>,
JpaSpecificationExecutor<SamplePersonAssocCar> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.data.VaadinSpringDataHelpers;

import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.PageRequest;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
Expand All @@ -45,6 +47,10 @@ public class MasterDetailView extends Div implements BeforeEnterObserver {

private TextField ape_color;
private ComboBox<String> batColors;

//private List<String> allBatColors;
private TextField allBatColorsCsv;
private TextField allCarColorsCsv;
//private TextField c;
private TextField d;
private TextField e;
Expand Down Expand Up @@ -74,6 +80,9 @@ public MasterDetailView(SamplePersonService samplePersonService) {
grid.addColumn("ape.color").setHeader("Ape Color").setAutoWidth(true);
grid.addColumn("favoriteBatColor").setAutoWidth(true);
//grid.addColumn("c").setAutoWidth(true);
//grid.addColumn("allBatColorsCsv").setAutoWidth(true);
grid.addColumn("allBatColorsCsv").setAutoWidth(true);
grid.addColumn("allCarColorsCsv").setAutoWidth(true);
grid.addColumn("d").setAutoWidth(true);
grid.addColumn("e").setAutoWidth(true);

Expand All @@ -100,6 +109,7 @@ public MasterDetailView(SamplePersonService samplePersonService) {
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 @@ -160,13 +170,21 @@ private void createEditorLayout(SplitLayout splitLayout) {

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

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

if(samplePerson!=null) {
String allBatColorsCsv = samplePerson.getAllBatColorsCsv();
String[] allBatColors = allBatColorsCsv.split(",");
batColors.setItems(allBatColors);
}

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

editorDiv.add(formLayout);
createButtonLayout(editorLayoutDiv);
Expand Down
12 changes: 9 additions & 3 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
insert into ape(version, id, color) values (1, 1, 'apricot')
insert into ape(version, id, color) values (1, 2, 'apple')
insert into car(version, id, color) values (1, 1, 'cocoa')
insert into car(version, id, color) values (1, 2, 'cream')
insert into sample_person(version, id, ape, cars, d, e) values (1, 1, 1, ARRAY [1, 2], 'd', 'e')
insert into bat(version, id, color, zooKeeper) values (1, 1, 'brown', 1)
insert into bat(version, id, color, zooKeeper) values (1, 2, 'blue', 1)
insert into sample_person(version, id, ape, d, e) values (1, 1, 1, 'd', 'e')
insert into sample_person(version, id, ape, d, e) values (1, 2, 2, 'd', 'e')
insert into sample_person_assoc_car(version, id, sample_person, car) values (1,1,1,1)
insert into sample_person_assoc_car(version, id, sample_person, car) values (1,2,1,2)
insert into sample_person_assoc_car(version, id, sample_person, car) values (1,3,2,2)
insert into bat(version, id, color, zookeeper) values (1, 1, 'brown', 1)
insert into bat(version, id, color, zookeeper) values (1, 2, 'blue', 1)
insert into bat(version, id, color, zookeeper) values (1, 2, 'burgundy', 2)

Binary file added src/main/resources/data.sql.mv.db
Binary file not shown.
Loading

0 comments on commit f31fea4

Please sign in to comment.