title: Spring @OneToMany problems
body: <p>Doing a project with parcel service. I created OrderItem API and Dispatcher API. Now, I want to connect then by relations. The idea is: dispatcher can have many orderItems. OrderItem can only have one dispatcher. If you delete dispatcher, his order items also has to go out.
  I have already created a little bit, but I'm so messed up here and can't finish this thing logically. Would someone give me some ideas on how I should attack this problem. </p>
  
  <p>Do I need to put relations both sides or only to one of them?</p>
  
  <p>When do I need to create constructors with arguments? Because in entity class you have to have no arg constructors...?</p>
  
  <p><strong>OrderItem class:</strong></p>
  
  <pre><code>@Entity
  public class OrderItem {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
      @NotBlank(message = "Order weight is required")
      private String weight;
      @NotBlank(message = "Order dimensions are required")
      private String dimensions;
      @NotBlank(message = "Order origin is required")
      private String origin;
      @NotBlank(message = "Order destination is required")
      private String destination;
      @NotNull(message = "Order comment cannot be null")
      private String comment;
  
      @ManyToOne
      private Dispatcher dispatcher;
  
      public OrderItem() {
  
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getWeight() {
          return weight;
      }
  
      public void setWeight(String weight) {
          this.weight = weight;
      }
  
      public String getDimensions() {
          return dimensions;
      }
  
      public void setDimensions(String dimensions) {
          this.dimensions = dimensions;
      }
  
      public String getOrigin() {
          return origin;
      }
  
      public void setOrigin(String origin) {
          this.origin = origin;
      }
  
      public String getDestination() {
          return destination;
      }
  
      public void setDestination(String destination) {
          this.destination = destination;
      }
  
      public String getComment() {
          return comment;
      }
  
      public void setComment(String comment) {
          this.comment = comment;
      }
  
      public Dispatcher getDispatcher() {
          return dispatcher;
      }
  
      public void setDispatcher(Dispatcher dispatcher) {
          this.dispatcher = dispatcher;
      }
  }
  </code></pre>
  
  <p><strong>OrderController class:</strong></p>
  
  <pre><code>@RestController
  @RequestMapping("/order")
  public class OrderController {
  
      @Autowired
      OrderService service;
  
      @Autowired
      private MapValidationErrorService mapValidationErrorService;
  
      @GetMapping("/{dispatcherId}/orders")
      public List&lt;OrderItem&gt; getAllOrderItems(@PathVariable int dispatcherId) {
          return service.getAllOrderItems(dispatcherId);
      }
  
      @PostMapping("/{dispatcherId}/orders")
      public ResponseEntity&lt;?&gt; saveOrder(@Valid @RequestBody OrderItem orderItem, @PathVariable int dispatcherId, BindingResult result) {
  
          ResponseEntity&lt;?&gt; errorMap = mapValidationErrorService.MapValidationService(result);
  
          if (errorMap != null) {
              return errorMap;
          }
  
          orderItem.setDispatcher(new Dispatcher(dispatcherId, "", "", ""));
          service.insertOrUpdate(orderItem);
          return new ResponseEntity&lt;String&gt;("Order was created successfully", HttpStatus.CREATED);
      }
  
      @PutMapping("/update")
      public ResponseEntity&lt;?&gt; updateOrder(@Valid @RequestBody OrderItem orderItem, BindingResult result) {
  
          ResponseEntity&lt;?&gt; errorMap = mapValidationErrorService.MapValidationService(result);
  
          if (errorMap != null) {
              return errorMap;
          }
  
          service.insertOrUpdate(orderItem);
          return new ResponseEntity&lt;String&gt;("Order was updated successfully", HttpStatus.OK);
      }
  
      @GetMapping("/all")
      public Iterable&lt;OrderItem&gt; getAllOrders() {
          return service.findAllOrders();
      }
  
      @DeleteMapping("/{orderId}")
      public ResponseEntity&lt;String&gt; deleteOrder(@PathVariable int orderId) {
  
          if (service.findById(orderId) == null) {
              throw new CustomErrorException("Order doesn't exist, check order id");
          }
  
          service.deleteOrder(orderId);
          return new ResponseEntity&lt;String&gt;("Order with ID " + orderId + " was deleted", HttpStatus.OK);
      }
  
      @GetMapping("/{orderId}")
      public ResponseEntity&lt;OrderItem&gt; getOrderById(@PathVariable int orderId) {
          OrderItem item = service.findById(orderId);
  
          if (service.findById(orderId) == null) {
              throw new CustomErrorException("Order id not found - " + orderId);
          }
  
          return new ResponseEntity&lt;OrderItem&gt;(item, HttpStatus.OK);
      }
  }
  </code></pre>
  
  <p><strong>Dispatcher class:</strong></p>
  
  <pre><code>@Entity
  public class Dispatcher {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
      @NotBlank(message = "Dispatcher first name is required")
      private String firstName;
      @NotBlank(message = "Dispatcher last name is required")
      private String lastName;
      @NotBlank(message = "Dispatcher email name is required")
      private String email;
      @NotBlank(message = "Dispatcher email is required")
      private String password;
      @NotBlank(message = "Dispatcher phone number is required")
      private String phoneNumber;
  
      public Dispatcher() {
  
      }
  
      public Dispatcher(int id, String firstName, String lastName, String email) {
          super();
          this.id = id;
          this.firstName = firstName;
          this.lastName = lastName;
          this.email = email;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public String getPhoneNumber() {
          return phoneNumber;
      }
  
      public void setPhoneNumber(String phoneNumber) {
          this.phoneNumber = phoneNumber;
      }
  }
  </code></pre>
  
  <p><strong>DispatcherController class:</strong></p>
  
  <pre><code>@RestController
  @RequestMapping("/dispatcher")
  public class DispatcherController {
  
      @Autowired
      DispatcherService service;
  
      @Autowired
      private MapValidationErrorService mapValidationErrorService;
  
      @PostMapping("/save")
      public ResponseEntity&lt;?&gt; saveDispatcher(@Valid @RequestBody Dispatcher dispatcher, BindingResult result) {
  
          ResponseEntity&lt;?&gt; errorMap = mapValidationErrorService.MapValidationService(result);
  
          if (errorMap != null) {
              return errorMap;
          }
  
          service.insertOrUpdate(dispatcher);
          return new ResponseEntity&lt;String&gt;("Dispatcher was created successfully", HttpStatus.CREATED);
      }
  
      @GetMapping("/all")
      public Iterable&lt;Dispatcher&gt; getAllDispatchers() {
          return service.findAllDispatchers();
      }
  
      @GetMapping("/{dispatcherId}")
      public ResponseEntity&lt;?&gt; getDispatcherById(@PathVariable int dispatcherId) {
  
          Dispatcher dispatcher = service.findById(dispatcherId);
  
          if (service.findById(dispatcherId) == null) {
              throw new CustomErrorException("Dispatcher id not found - " + dispatcherId);
          }
  
          return new ResponseEntity&lt;Dispatcher&gt;(dispatcher, HttpStatus.OK);
      }
  
      @DeleteMapping("/{dispatcherId}")
      public ResponseEntity&lt;?&gt; deleteDispatcher(@PathVariable int dispatcherId) {
  
          if (service.findById(dispatcherId) == null) {
              throw new CustomErrorException("Dispatcher doesn't exist, check dispatcher id");
          }
  
          service.deleteDispatcher(dispatcherId);
          return new ResponseEntity&lt;String&gt;("Order with ID " + dispatcherId + " was deleted", HttpStatus.OK);
      }
  
      @PutMapping("/update")
      public ResponseEntity&lt;?&gt; updateDispatcher(@Valid @RequestBody Dispatcher dispatcher, BindingResult result) {
  
          ResponseEntity&lt;?&gt; errorMap = mapValidationErrorService.MapValidationService(result);
  
          if (errorMap != null) {
              return errorMap;
          }
  
          service.insertOrUpdate(dispatcher);
          return new ResponseEntity&lt;String&gt;("Dispatcher was updated successfully", HttpStatus.OK);
      }
  }
  </code></pre>