Search code examples
javaspringannotationsone-to-many

Spring @OneToMany problems


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.

Do I need to put relations both sides or only to one of them?

When do I need to create constructors with arguments? Because in entity class you have to have no arg constructors...?

OrderItem class:

@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;
    }
}

OrderController class:

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService service;

    @Autowired
    private MapValidationErrorService mapValidationErrorService;

    @GetMapping("/{dispatcherId}/orders")
    public List<OrderItem> getAllOrderItems(@PathVariable int dispatcherId) {
        return service.getAllOrderItems(dispatcherId);
    }

    @PostMapping("/{dispatcherId}/orders")
    public ResponseEntity<?> saveOrder(@Valid @RequestBody OrderItem orderItem, @PathVariable int dispatcherId, BindingResult result) {

        ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);

        if (errorMap != null) {
            return errorMap;
        }

        orderItem.setDispatcher(new Dispatcher(dispatcherId, "", "", ""));
        service.insertOrUpdate(orderItem);
        return new ResponseEntity<String>("Order was created successfully", HttpStatus.CREATED);
    }

    @PutMapping("/update")
    public ResponseEntity<?> updateOrder(@Valid @RequestBody OrderItem orderItem, BindingResult result) {

        ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);

        if (errorMap != null) {
            return errorMap;
        }

        service.insertOrUpdate(orderItem);
        return new ResponseEntity<String>("Order was updated successfully", HttpStatus.OK);
    }

    @GetMapping("/all")
    public Iterable<OrderItem> getAllOrders() {
        return service.findAllOrders();
    }

    @DeleteMapping("/{orderId}")
    public ResponseEntity<String> 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<String>("Order with ID " + orderId + " was deleted", HttpStatus.OK);
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<OrderItem> 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<OrderItem>(item, HttpStatus.OK);
    }
}

Dispatcher class:

@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;
    }
}

DispatcherController class:

@RestController
@RequestMapping("/dispatcher")
public class DispatcherController {

    @Autowired
    DispatcherService service;

    @Autowired
    private MapValidationErrorService mapValidationErrorService;

    @PostMapping("/save")
    public ResponseEntity<?> saveDispatcher(@Valid @RequestBody Dispatcher dispatcher, BindingResult result) {

        ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);

        if (errorMap != null) {
            return errorMap;
        }

        service.insertOrUpdate(dispatcher);
        return new ResponseEntity<String>("Dispatcher was created successfully", HttpStatus.CREATED);
    }

    @GetMapping("/all")
    public Iterable<Dispatcher> getAllDispatchers() {
        return service.findAllDispatchers();
    }

    @GetMapping("/{dispatcherId}")
    public ResponseEntity<?> 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<Dispatcher>(dispatcher, HttpStatus.OK);
    }

    @DeleteMapping("/{dispatcherId}")
    public ResponseEntity<?> 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<String>("Order with ID " + dispatcherId + " was deleted", HttpStatus.OK);
    }

    @PutMapping("/update")
    public ResponseEntity<?> updateDispatcher(@Valid @RequestBody Dispatcher dispatcher, BindingResult result) {

        ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);

        if (errorMap != null) {
            return errorMap;
        }

        service.insertOrUpdate(dispatcher);
        return new ResponseEntity<String>("Dispatcher was updated successfully", HttpStatus.OK);
    }
}

Solution

  • I think you have defined the relationship incorrectly. And yes you need to have no-args constructor. This helps hibernate to map the values from database to java objects when retrieving data from the database

    Assuming you are going for a uni-directional mapping,

    @Entity
    public class OrderItem {
    
        @ManyToOne( cascade = CascadeType.ALL )
        @JoinColumn(name = <foriegn_key_column in orderItem table i.e. id>)
        private Dispatcher dispatcher;
    }
    
    @Entity
    public class Dispatcher {
    
        private List<OrderItem > orders;
    }