This should be a simple thing to do! But I've been unable to find an answer so far. Either I'm missing something obvious, or else I'm missing something obvious...
I have a class, say Person. With three fields - "id", "name" and "reputation". Let's say that I am willing to take updates to "name" but not to "reputation". I'd like Spring Data to fetch the value of "reputation" when retrieving from DB, but ignore it when I'm saving the bean.
@Transient annotation is there, but then Spring completely ignores the field and doesn't populate it at all. Ideally, I'm looking for something like @ReadOnly annotation.
More details
Generally, we use @Transactional(readOnly = true) for search or retrieval operation to make sure we can only perform the read-only operation. We can override readOnly behavior using @Modifying annotation.
In your entity add an EntityListener like this: @Entity @EntityListeners(PreventAnyUpdate. class) public class YourEntity { // ... } This will create a bullet proof safety net for your entity with JPA lifecycle listeners.
Spring @Transactional annotationThe readOnly attribute can further be used by Spring to optimize the underlying data access layer operations. Prior to Spring 5.1, when using Hibernate, the readOnly attribute of the @Transactional annotation was only setting the current Session flush mode to FlushType.
The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method. We have two ways to rollback a transaction: declarative and programmatic. In the declarative approach, we annotate the methods with the @Transactional annotation.
You can use a transient property without a setter. That transient property would return the db property value that is to be protected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With