Reputation: 1985
I'm looking for a way to prevent objects from updating with spring-data. I've found some topics about this, but none of them have a solution.
I have an object, that can be created and read. That's it. I do not want to support modifications/updates. I manage/generate the database-id myself.
I would love to use org.springframework.data.repository.Repository
interface (or JpaRepository
/CrudRepository
if I can somehow change the <S extends T> S save(S entity);
logic to prevent update), because I love the clean findBy... interface notations.
The best solution I've found so far is to implement Persistable
and overwrite the isNew(…)
to always return true. But that's just crappy, because the object is not "new" if it's read-only.
Is there any clean way to achieve this?
Upvotes: 1
Views: 2919
Reputation: 30279
You can, for example, simply use Repository event handler to restrict update and delete operations:
@RepositoryEventHandler(MyEntity.class)
public class MyEntityEventHandler {
@HandleBeforeSave
public void handleUpdate(MyEntity entity) {
throw new HttpRequestMethodNotSupportedException("Update")
}
@HandleBeforeDelete
public void handleDelete(MyEntity entity) {
throw new HttpRequestMethodNotSupportedException("Delete")
}
}
Upvotes: 1