Ok, let me know if I misunderstood the questions but I think I understand.
Specifically with JPA with beans still attached to the persistence context, on flush mutated/dirty beans are automatically persisted. In comparison with Ebean where we expect to explicitly save() etc.
The question is, can Ebean simulate or do this such that we don't need to explicitly save() beans but instead auto persist mutated beans on flush/commit. (I'll sort of answer below).
Ok, interesting. I don't think I have seen code quite like that. This is an interesting point and if possible I'd love to see a little example if you are able to share (obviously it might be code you are not allowed to share).
> P.S.: currently we use Eclipselink ...
Yes. I think we are seeing more of this.
Right, back to your original question.
So does Ebean do this today? No it does not.
Could we emulate it? Maybe we could ...
Ebean has a PersistenceContext (io.ebean.bean.PersistenceContext, io.ebeaninternal.server.transaction.DefaultPersistenceContext). This PersistenceContext is held on the transaction (transaction scoped persistence context) and it holds all the beans that were loaded. These are the beans we would mutate (for updates).
For the simple case of updating mutated beans: It would be pretty much ... get the beans from the persistence context that are in dirty state, determine the order that they should be persisted (maybe the ordering of bean types does not matter per say), persist them - call database.save(dirtyBean).
What needs to be added, tested, thought about would be handling of insert and explicit delete's (orphan removal would be handled by database.save(
dirtyBean
)). That is, I think it could be that we can still just do database.insert(bean) and database.delete(bean) as per normal - with batch persist on then these just go to the internal BatchControl held by the transaction so that should be fine. There would be a question around if inserted beans should be added to the persistence context after a flush() [ideally they probably should for the case they are subsequently mutated and should be an update in a subsequent flush].
So emulating it might well be possible and not too tricky ... hmmm.
Note as a side point, that on SpiTransaction we have getPersistenceContext() and setPersistenceContext() - at this stage we have not had a good strong argument for exposing this in public API (via Transaction). If we got this all working then we could consider doing that to support "extended persistence context" - loading beans using one transaction, later transfering the persistence context to another transaction for doing more fetching and/or persisting.
Ok, probably worth writing some tests to simulate this and where the complexity is. If we did manage to do this nicely then this likely would be an opt in option on a per transaction basis and globally (all transactions).
Note that when we have streaming queries findEach, findIterate, findStream ... these can use multiple persistence contexts (e.g. when iterating millions then we don't want all beans held in memory in the single persistence context) so we couldn't use this feature there but that should be expected.
Next steps:
Create a test or 2 that "simulate" this and go from there.