当我不希望Spring Data JPA更新实体时,它会更新实体 [英] Spring Data JPA updates entity when I don't want it to

查看:182
本文介绍了当我不希望Spring Data JPA更新实体时,它会更新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我想检索数据,但是在返回数据之前,我想在不保存/持久化的情况下进行更改.

I have a situation where I want to retrieve data, but before it is returned I want to change it without saving/persisting.

这是我的休息控制器:

@RestController
@RequestMapping("api/drawing-releases")
@CrossOrigin("*")
public class DrawingReleaseRestController {
    @Autowired
    private DrawingReleaseService service;

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    @JsonView(View.DrawingReleaseView.class)
    public ResponseEntity<DrawingRelease> getWithId(@PathVariable int id) throws EntityNotFoundException {
      return new ResponseEntity<>(service.getByID(id),HttpStatus.OK);
    }
}

这是服务实现:

@Service
public class DrawingReleaseServiceImpl extends DrawingFunctions implements DrawingReleaseService {
    /**
     * Logging Manager
     */
    private static final Logger LOGGER=LogManager.getLogger();

    @Autowired
    private DrawingReleaseRepository repository;

    @Override
    public DrawingRelease getByID(int id) throws EntityNotFoundException {
        Optional<DrawingRelease> opt = repository.findById(id);
        ...
        // manipulate the data here
        ...
        return opt.get();
    }
}

最初,我在服务上具有@Transactional批注.通过删除它,并且没有在getByID方法上使用它,我首先想到的是,该会话将按照答案

Intitially I had the @Transactional annotation on the service. By removing that and not having it on the getByID method, I was first thinking that the session would open and close with the repository as indicated by the answer here.

那是行不通的,我在注释中看到会话在HTTP请求处理的整个过程中持续".因此,在我添加的服务中

That did not work, and I saw in the comments that "the session lasts for the entire duration of HTTP request processing." So, in the service I added

@Autowired
private EntityManager em;

,然后在我添加的getByID方法中

and then in the getByID method I added

em.close();

在进行任何更改之前.但是,我所做的任何更改仍会保留.

before I made any changes. However, any changes I make are still being persisted.

有什么方法可以在服务层中进行未保存的更改?我想我可以创建一些映射实体(但不是实体)的POJO,然后复制数据并返回那些对象,但是看来我不必要做类似的事情.

Is there any way that I can make unsaved changes in my service layer? I suppose I could create some POJOs that mirror the entities (but aren't entities) and then copy the data and return those objects, but it doesn't seem like I should have to do something like that.

推荐答案

您说要获取一个实体,然后更改数据,然后在不保留该实体的情况下返回它,但是恕我直言,您不应该将实体用于这.您为什么要更改数据以及用户将如何处理数据,因为数据不再代表数据库中的内容.将要进行哪些其他更改,或者哪些其他假设(这是一个数据库对象)将无效?

You say you want to get an Entity and then change the data and then return it without the entity being persisted but IMHO you shouldn't be using the entity for this. Why are you changing the data and what is the user going to do with it since it no longer represents what's in the database. What other changes are going to be made or what other assumptions (this is a database object) are going to be invalid?

如果您正在使用数据库对象派生其他对象,则您应该为此使用其他类.这是常见的,称为数据传输对象(DTO),可以直接使用投影从spring-data-jpa创建它们.您的Dto对象将采用实体作为构造函数,并填写想要从该实体获取的任何属性. Dto不是持久对象,因此保存它周围没有任何问题.

If you are using a database object to derive a different object you should have a different class for this purpose. This is common and is called a Data Transfer Object (DTO) and they can be created from spring-data-jpa directly using projection. Your Dto object would take an entity as a constructor and fill out whatever properties it wants to from the entity. The Dto is not a persistent object so there are no issues around it being saved.

更清晰,更易理解的设计,并且与 SOLID原理和代码结构.

Much clearer and more understandable design and consistent with SOLID Principles and code structure.

这篇关于当我不希望Spring Data JPA更新实体时,它会更新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆