gpt4 book ai didi

java - Spring @OneToMany 问题

转载 作者:行者123 更新时间:2023-12-01 17:27:24 25 4
gpt4 key购买 nike

做一个包裹服务的项目。我创建了 OrderItem API 和 Dispatcher API。现在,我想通过关系来联系。这个想法是:调度程序可以有许多 orderItems。 OrderItem 只能有一个调度程序。如果你删除调度员,他的订单项目也必须出去。我已经创建了一点,但是我这里太乱了,无法逻辑地完成这件事。有人可以给我一些关于如何解决这个问题的想法吗?

我需要将关系添加到两侧还是只添加到其中之一?

什么时候需要创建带参数的构造函数?因为在实体类中你必须没有参数构造函数......?

OrderItem 类:

@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 类:

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

调度程序类:

@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 类:

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

最佳答案

我认为您错误地定义了这种关系。是的,你需要有无参数构造函数。这有助于 hibernate 在从数据库检索数据时将数据库中的值映射到 java 对象

假设您要进行单向映射,

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

关于java - Spring @OneToMany 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61192047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com