gpt4 book ai didi

Java JPA - 如何使用列表放置对象并使用 CRUD 更新列表对象?

转载 作者:行者123 更新时间:2023-12-02 10:13:11 26 4
gpt4 key购买 nike

我有一个对象,其中包含我似乎无法正确更新的其他对象的列表。我可以使用对象列表(ProductItemQuantity)创建对象(Product),没有问题。我还可以对对象列表执行 PUT,但我执行 PUT 的所有操作都会创建一个新的对象列表。我希望更新我提供的对象列表,而不是每次放置父对象时都创建一个新列表。

如果我向 ProductItemQuantity 添加 ID,则会出现异常:

detached entity passed to persist

这是我的类(class):

产品.java

    @Entity 
public class Product {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Organization org;
private String barCode;
private String name;
@ManyToOne(cascade=CascadeType.MERGE)
private Status status;
@ManyToMany(cascade=CascadeType.MERGE)
private List<Fee> fees;
@ManyToMany(cascade=CascadeType.ALL)
private List<Note> notes;
@ManyToMany(cascade=CascadeType.ALL)
private List<ProductItemQuantity> productItems;
private Integer stock;
private BigDecimal msrp;
@CreationTimestamp
private LocalDateTime createdOn;
@UpdateTimestamp
private LocalDateTime updatedOn;

// Getter & Setters

ProductItemQuantity.java

@Entity 
public class ProductItemQuantity {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private Integer count;
@ManyToOne
private ProductItem productItem;
@CreationTimestamp
private LocalDateTime createdOn;
@UpdateTimestamp
private LocalDateTime updatedOn;

// Getters / setters

ProductItem.java

@Entity 
public class ProductItem {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Organization org;
@ManyToOne
private Supplier supplier;
private String barCode;
private String description;
private String name;
private Integer stock;
private Integer caseQty;
private BigDecimal caseCost;
@ManyToMany(cascade=CascadeType.ALL)
private List<Note> notes;
@CreationTimestamp
private LocalDateTime createdOn;
@UpdateTimestamp
private LocalDateTime updatedOn;

ProductController.java

@PutMapping("/{id}")
public Product update(@RequestBody Product product, @PathVariable long id) {


Product savedProduct = productService.save(product);


return savedProduct;
}

工作 CRUD PUT 请求:http://localhost:8080/product/1

{
"barcode":"12347163",
"name":"Product 1",
"stock": 12,
"msrp": 29.99,
"org": {
"id":1
},
"status":{
"id":1
},
"productItems":[{
"count":30
},{
"count":30
}
],
"fees":[{
"id":1

},{
"id":2

}],
"notes":[{
"title":"Product Created",
"description":"Note created by user X on 12/16/2019 11:00PM"
},{
"title":"Product Updated",
"description":"Product updated stock by user X on 12/16/2019 11:00PM"
}]
}

损坏的 CRUD PUT 请求:http://localhost:8080/product/1

{
"barcode":"12347163",
"name":"Product 1",
"stock": 12,
"msrp": 29.99,
"org": {
"id":1
},
"status":{
"id":1
},
"productItems":[{
"id":1,
"count":30
},{
"id":2,
"count":30
}
],
"fees":[{
"id":1

},{
"id":2

}],
"notes":[{
"title":"Product Created",
"description":"Note created by user X on 12/16/2019 11:00PM"
},{
"title":"Product Updated",
"description":"Product updated stock by user X on 12/16/2019 11:00PM"
}]
}

最佳答案

您的对象已分离,因为关系(OneToMany、ManyToMany)仅从一个方向设置。为了保留它们,您必须设置双向关系。您的关系是单向的,因为解析器( jackson )将生成以下对象:

Product product = new Product();
Fee fee = new Fee();
fee.setId(1);
product.setFees(Arrays.asList(fee));

在双向关系中,双方都必须设置:

product.getFees().forEach(fee-> fee.getProducts().add(product));

将持久性对象与 Controller 对象分开是一个很好的做法,因为持久性对象也处理关系。

根据我的经验,如果你想使用GenerateValue,你必须首先从数据库中选择实体,然后修改它。如果您希望 hibernate 生成它,那么创建一个新对象并在其上设置 id 没有多大意义。

因此,您可能需要先进行选择:

列出费用=//选择产品费用列表中id为所有费用

及之后:

product.setFees(fees);
fees.forEach(fee -> fee.getProducts().add(product));

您的方法是 PUT,因此您不应直接保存产品对象(它将在数据库中创建一个新条目)。

@PutMapping("/{id}")
public Product update(@RequestBody Product product, @PathVariable long id) {
Optional<Product> originalProductOptional = productRepository.findById(id);
// you should add a check here : if originalProduct is not found, return 404
Product originalProduct = originalProductOptional.get();
originalProduct.setName(product.getName());
// here update all fields and relations

productRepository.save(originalProduct);
return originalProduct;
}

关于Java JPA - 如何使用列表放置对象并使用 CRUD 更新列表对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54853631/

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