@ManyToOne 如何使 Json 引用现有的外键?
@ManyToOne how to make Json refer to existing foreign key?
我正在制作 Spring 启动 RESTful 应用程序。我有两个实体:访客:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
private String gender;
}
和产品
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int qty;
private int price;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "customer_id", referencedColumnName = "id")
private Customer customer;
}
这就是我试图通过控制器将 Json 推入 OrderRequest 对象的方式:
@PostMapping("/placeOrder")
public Product saveOrder(@RequestBody OrderRequest request){
return productRepository.save(request.getProduct());
}
订单请求class:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderRequest {
private Product product;
}
存储库 class 是标准的:
public interface ProductRepository extends JpaRepository<Product, Integer> {}
public interface CustomerRepository extends JpaRepository<Customer, Integer> {}
我希望 json 看起来像这样:
{
"product":{
"name":"Mobile",
"price":10000,
"qty":1,
"idCustomer": 1
}
}
问题:如何使用客户 table 中已有的行,将 Json 发送到服务器,其中我仅指示产品参数和一个将引用客户行的外键参数?? 谢谢
在控制器层公开 persistence-level 实体不是最佳做法,因为您的 API 与数据的内部表示耦合。
实现您的要求的常用方法是使用传输对象模式。也就是说,创建单独的 classes 以在 API 中使用。在您的用例中,您可以创建具有以下结构的 ProductTO class:
public class ProductTO {
private int id;
private String name;
private int qty;
private int price;
private int customerId;
}
然后你可以手动映射ProductTO和Product之间的字段,或者使用任何映射库(例如:MapStruct)自动复制两个classes中同名字段的值。
需要手动分配外键:
Customer customer = this.customerRepo.findById(productTO.getCustomerId()).orElseThrow(() -> new YourException());
product.setCustomer(customer);
我正在制作 Spring 启动 RESTful 应用程序。我有两个实体:访客:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
private String gender;
}
和产品
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int qty;
private int price;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "customer_id", referencedColumnName = "id")
private Customer customer;
}
这就是我试图通过控制器将 Json 推入 OrderRequest 对象的方式:
@PostMapping("/placeOrder")
public Product saveOrder(@RequestBody OrderRequest request){
return productRepository.save(request.getProduct());
}
订单请求class:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderRequest {
private Product product;
}
存储库 class 是标准的:
public interface ProductRepository extends JpaRepository<Product, Integer> {}
public interface CustomerRepository extends JpaRepository<Customer, Integer> {}
我希望 json 看起来像这样:
{
"product":{
"name":"Mobile",
"price":10000,
"qty":1,
"idCustomer": 1
}
}
问题:如何使用客户 table 中已有的行,将 Json 发送到服务器,其中我仅指示产品参数和一个将引用客户行的外键参数?? 谢谢
在控制器层公开 persistence-level 实体不是最佳做法,因为您的 API 与数据的内部表示耦合。
实现您的要求的常用方法是使用传输对象模式。也就是说,创建单独的 classes 以在 API 中使用。在您的用例中,您可以创建具有以下结构的 ProductTO class:
public class ProductTO {
private int id;
private String name;
private int qty;
private int price;
private int customerId;
}
然后你可以手动映射ProductTO和Product之间的字段,或者使用任何映射库(例如:MapStruct)自动复制两个classes中同名字段的值。
需要手动分配外键:
Customer customer = this.customerRepo.findById(productTO.getCustomerId()).orElseThrow(() -> new YourException());
product.setCustomer(customer);