简体   繁体   English

Spring boot 如何编辑实体

[英]Spring boot how to edit entity

I am using spring boot with spring data jpa and postgre.我正在使用带有 spring 数据 jpa 和 postgre 的 spring boot。 I have "item" entity that has price, quantity, auto generated int id and order that it belongs to.我有“项目”实体,它具有价格、数量、自动生成的 int id 和它所属的订单。 I've searched how to edit that entity changing its price and quantity only, without making new entity and the only answer I got is to get the entity from the db and set each property to the new one then save it.我已经搜索了如何编辑该实体,只更改其价格和数量,而不创建新实体,我得到的唯一答案是从数据库中获取实体并将每个属性设置为新属性,然后保存它。 But if i have 6 other properties except price and quantity that means in the update method i will set a property 8 times and this seems to me like way too much boilerplate code for spring.但是如果我有 6 个除了价格和数量之外的其他属性,这意味着在更新方法中我将设置一个属性 8 次,这在我看来就像太多的 spring 样板代码。 My question is: Is there better/default way to do that?我的问题是:有没有更好/默认的方法来做到这一点?

You can provide a copy constructor :您可以提供一个复制构造函数

public Item(Item item) {
    this(item.price, item.quantity); 
}

or use org.springframework.beans.BeanUtils method:或使用org.springframework.beans.BeanUtils方法:

BeanUtils.copyProperties(sourceItem, targetItem, "id"); 

Then in controller:然后在控制器中:

@RestController
@RequestMapping("/items")
public class ItemController {

    @Autoware
    private ItemRepo repo;

    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable("id") Item targetItem,  @RequestBody Item sourceItem) {
        BeanUtils.copyProperties(sourceItem, targetItem, "id");
        return ResponseEntity.ok(repo.save(targetItem));
    }
}

No, you don't need to set anything for 8 times.不,您不需要设置 8 次。 If you want to change price and quantity only, just change those two.如果您只想更改价格和数量,只需更改这两个即可。 Put it in a @Transactional method:把它放在@Transactional 方法中:

@Transactional
public void updateItem(Item item){
    // ....
    // EntityManager em;
    // ....

    // Get 'item' into 'managed' state
    if(!em.contains(item)){
        item = em.merge(item);
    }

    item.price = newPrice;
    item.quantity = newQuantity;
    // You don't even need to call save(), JPA provider/Hibernate will do it automatically.
}

This example will generate a SELECT and a UPDATE query.此示例将生成一个SELECT和一个UPDATE查询。 And that's all.仅此而已。

Try using @Query annotation and define your update statement尝试使用@Query注释并定义您的update语句

 @Modifying
 @Transactional
 @Query("update Site site set site.name=:name where site.id=:id")
 void updateJustNameById(@Param("id")Long id, @Param("name")String name);

You should use spring data rest which handles all of this by itself.您应该使用 spring data rest 它自己处理所有这些。 you just have to call a patch request at the specified URL and provide the changed entity properties.您只需要在指定的 URL 上调用补丁请求并提供更改的实体属性。 if you have some knowledge of spring data rest have a look at https://github.com/ArslanAnjum/angularSpringApi .如果您对 spring 数据休息有一些了解,请查看https://github.com/ArslanAnjum/angularSpringApi

Just use this @DynamicUpdate in your Entity class只需在您的实体类中使用此 @DynamicUpdate

@DynamicUpdate
public class Item{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM