-
-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Immutable entities, bean state and save/update/insert operations #3520
Comments
Hello, I will try to answer some of your questions.
You can only use save, if the internal state is also be cloned. Otherwise, you have to do stateless updates or maybe even Dtos may be better for this use case.
Mostly overhead. Ebean is designed to detect changes and only update these changes on save. So there is an EntityBeanIntercept, the ebean enhancer, BeanCollections with change detection and so on. The overhead can be reduced with setReadonly on the query
With the state (EntityBeanIntercept) tracks, which properties are dirty and ebean derives, which insert/update query has to be executed
Nearly every change on the bean affects the beanstate. There is You can also directly access the complete beanState, when you cast your bean to
Yes (unless you do not have other generated fields, like lastModified)
😉 I would say, your use case should be possible (clone the EBI in your factory and calling save can work), but as mentioned above, ebean is not primary designed for this use case. There is the persistence context, that tracks beans with same ID and there is also lazy loading, which should not be triggered on cloning. So, you will need a good understanding, what's going on behind the scenes... Take a look also on readOnly queries and try to implement a PoC on this. Hope that helps Roland |
This reminds me of a conversation I had on reddit recently on this topic. I wonder if you are the same person? Anyway ...
The DEFAULT is to only update properties that have changed. This is configuration that can change globally (via DatabaseBuilder.setUpdateAllPropertiesInBatch()) and per transaction via Interestingly this wasn't always the default, we changed to this a fairly long time ago (maybe 10 years, I can't remember). The argument for this default is that in practice we still get good hit ratio on PreparedStatements because per use case typically the same properties are mutated. So yes a lower hit ratio on PreparedStatements but in practice it's still a good hit ratio and with only updating dirty properties less data is sent over to the database and it's cheaper in terms of network and database - so the argument is that it's a net win to only update the dirty properties. Devs who disagree can set the global default to include all [loaded*] properties in the update. For ebean it's the loaded* properties because ebean supports partial objects [like eclipselink, unlike hibernate].
I've been thinking about this for a while. This might get a lot more interesting when Java gets "withers" but yes effectively the bean state would need to be transferred to a "transformed entity".
Note that "Unmodifiable graphs" are work-in-progress improvement on readOnly so just note that. |
Thanks for the answers! I'm struggling a bit trying to copy and modify the entity state.
How do I clone the entity state, modify it and apply it to another entity? I can pass the bean state to my builder class but how do I modify it and how do I apply it to the newly created entity? Can I use the BeanState API to manually change the state? There is only the Here is an (minimal) example of our entity classes:
Not the same person 🙂 |
We are considering switching from Spring Data JPA to Ebean, but I'm missing some documentation regarding the internal state of entity beans and its effect on save/update/insert operations.
Here are my questions:
It would be great to have better documentation regarding entity state and save/update/insert operations.
The text was updated successfully, but these errors were encountered: