Is it possible to change instance instance with version and get version with be-be-incremented index without using flash? Because from what I read, I'm afraid that flash is not a good practice, because it affects performance badly or even data corruption? Im not sure: D
Here is a simple code, as well as the output as a comment:
public void modifyHeaderAndGetUpdatedVersion() { String id = "3b373f6a-9cd1-4c9c-9d46-240de37f6b0f"; ReceivingGood receivingGood = em.find(ReceivingGood.class, id); System.out.println("the version before modification : " + receivingGood.getVersion()); receivingGood.setTransactionNumber("NUM001xyz"); System.out.println("the version after modification : " + receivingGood.getVersion()); receivingGood = em.merge(receivingGood); System.out.println("after merge the modification, the version is : " + receivingGood.getVersion()); em.flush(); System.out.println("after flushing the modification, the version is finally : " + receivingGood.getVersion()); }
In my test, the version was increased after the flush. The instance returned from the merge operation does not have an incremental version.
But in my case, I would like to return the object to my webui in the form of DTO, and the entity must have a version after the / commit flush before converting it to DTO and returning it to the user interface. And then the user interface can have the latest version and will transmit this version for the next presentation.
Is there a way when I can get the latest version without a flash?
Thanks!
UPDATE
In my experience, manually increasing this can be problematic, as can be seen from the example below. In this example, we have 2 flushes.
The first is the synchronization of changes to the db connection, so that a call to a stored procedure from the same connection can see the changes made to the entityManager.
The second flash is called to get the final version. And we see that it doubles . Thus, getting the version only from manual increment without flushing will not work in this state, since we must really count how many flushes are performed.
public void modifyHeaderAndGetUpdatedVersionWith2Flushes() { String id = "3b373f6a-9cd1-4c9c-9d46-240de37f6b0f"; ReceivingGood receivingGood = em.find(ReceivingGood.class, id); System.out.println("the version before modification : " + receivingGood.getVersion());
Does this mean that I really have to depend on the flash at the end to get the latest version for the modified object?
UPDATE 2
Here is a simple example of a service method that will update a ReceivingGood object and should return a DTO that has the latest version.
public ReceivingGoodDTO update(ReceivingGood entity) {
and here is an example that uses this method:
@Transactional public ReceivingGoodDTO doSomethingInTheServiceAndReturnDTO() {