Hendrik Jungnitsch
2022-09-07 86a01d37d927894f9ccd672504a05658286fa529
exercise04
3 Dateien geändert
42 ■■■■■ Geänderte Dateien
src/main/java/de/gedoplan/seminar/jpa/exercise/domain/Highway.java 15 ●●●●● Patch | Ansicht | Raw | Blame | Historie
src/main/java/de/gedoplan/seminar/jpa/exercise/domain/Junction.java 16 ●●●●● Patch | Ansicht | Raw | Blame | Historie
src/main/java/de/gedoplan/seminar/jpa/exercise/rest/JunctionResource.java 11 ●●●●● Patch | Ansicht | Raw | Blame | Historie
src/main/java/de/gedoplan/seminar/jpa/exercise/domain/Highway.java
@@ -1,8 +1,14 @@
package de.gedoplan.seminar.jpa.exercise.domain;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = Highway.TABLE_NAME)
@@ -15,6 +21,10 @@
    private String name;
    private String origin;
    private String destination;
    @JsonIgnoreProperties("highway")
    @OneToMany(mappedBy = "highway")
    private List<Junction> junctions = new ArrayList<>();
    
    public Highway() {
    }
@@ -58,6 +68,11 @@
        return this.destination;
    }
    public List<Junction> getJunctions() {
        return junctions;
    }
    @Override
    public String toString() {
        return "Highway [id=" + id + ", name=" + name + ", origin=" + origin + ", destination=" + destination + "]";
src/main/java/de/gedoplan/seminar/jpa/exercise/domain/Junction.java
@@ -8,7 +8,10 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = Junction.TABLE_NAME)
@@ -26,6 +29,10 @@
    private JunctionKind kind;
    private String no;
    @JsonIgnoreProperties("junctions")
    @ManyToOne
    private Highway highway;
    public Junction() {
@@ -69,6 +76,15 @@
        this.no = no;
    }
    public Highway getHighway() {
        return highway;
    }
    public void setHighway(Highway highway) {
        this.highway = highway;
    }
    @Override
    public int hashCode() {
        return Objects.hash(id);
src/main/java/de/gedoplan/seminar/jpa/exercise/rest/JunctionResource.java
@@ -6,6 +6,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@@ -16,7 +17,9 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import de.gedoplan.seminar.jpa.exercise.domain.Highway;
import de.gedoplan.seminar.jpa.exercise.domain.Junction;
import de.gedoplan.seminar.jpa.exercise.repository.HighwayRepository;
import de.gedoplan.seminar.jpa.exercise.repository.JunctionRepository;
@RestController
@@ -30,6 +33,9 @@
  @Autowired
  JunctionRepository junctionRepository;
  @Autowired
  HighwayRepository highwayRepository;
  /**
   * Exercise JPA_BASICS_02: Insert test data.
@@ -56,8 +62,11 @@
    return junction.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
  }
  
  @Transactional
  @PutMapping("/{junctionId}/assignToHighway/{highwayId}")
  public void assignToHighway(@PathVariable Integer junctionId, @PathVariable Integer highwayId) {
      Highway highwayRef = highwayRepository.getReferenceById(highwayId);
      Junction junction = junctionRepository.findById(junctionId).orElseThrow();
      junction.setHighway(highwayRef);
  }
}