Reputation: 2797
In one application there are several entity classes, for which update statements to the database shall be forbidden. I want to insert into and read from the database, but never do any updates on existing records.
Is there a way to mark a whole
@Entity
class asupdatable=false
?
One could annotate every single field with @Column(updatable=false)
or similar annotations, but for obvious reasons I'd like to avoid this.
Getting rid of the setter methods is not an option as well, because the entity classes are also used as DTOs and other parts of the application need those setters. so this would cause to much refactoring of existing code.
Is there another simple and clean way to achieve what I'd like to have with JPA 2.1 / EclipseLink (+Extensions)
Upvotes: 5
Views: 1816
Reputation: 9022
You could use @PreUpdate
for that:
@Entity
public class ReadOnlyEntity {
@PreUpdate
private void preUpdate() {
throw new UnsupportedOperationException();
}
}
That way you can never update (write) that entity.
Upvotes: 7