Cherry
Cherry

Reputation: 33534

How disable entity updating based on one field?

Say that there is a entity like Document and it has type field. When type is draft is can be updated. But when type is created hibernate should not save document with any modified values. Is it possible with hibernate?

Upvotes: 2

Views: 292

Answers (1)

Stanislav
Stanislav

Reputation: 28106

You can make an immutable entity, with the @Immutable annotation, in that case, you are not able to modify the entity, then it's persisted.

Another solution, is to make an entity read-only via session, as it's shown in the official documentation.

One more solution, is to provide an EntityListener for your entity, like:

@Entity
@EntityListeners(MakeReadOnly.class)
public class SomeEntity {
    // ...
}

public class MakeReadOnly {
    @PreUpdate
    void onPreUpdate(Object o) {
        //according to filed value throw new RuntimeException("...");
    }
}

Upvotes: 3

Related Questions