Junior — Middle
Which annotation in Java prohibits changing the value of a field inside an entity?
sobes.tech AI
Answer from AI
In Java, to prevent modification of a field's value within an entity, the @Immutable annotation from the Hibernate library is usually used. It indicates that the object is immutable after creation, and Hibernate will not attempt to update the fields of this entity.
Example:
import org.hibernate.annotations.Immutable;
@Immutable
@Entity
public class MyEntity {
@Id
private Long id;
private String name;
// getters without setters or with private setters
}
If not using Hibernate but plain Java, the final keyword can be used to make a field immutable, which prohibits reassignment of the field after initialization.