Java MCQ: Which annotation is used to mark an entity as read-only in Hibernate?
Answer:
Explanation:
The @Immutable
annotation in Hibernate is used to mark an entity as read-only. When an entity is annotated with @Immutable
, Hibernate will not allow any modifications to the entity’s data after it has been persisted. This annotation is useful for entities that represent data that should not be changed, such as historical records or reference data.
Here’s an example:
@Entity
@Immutable
public class HistoricalRecord {
@Id
private Long id;
private String data;
// Getters and setters
}
In this example, the HistoricalRecord
entity is marked as immutable using the @Immutable
annotation, meaning that once the data is saved, it cannot be updated.
The @Immutable
annotation helps ensure data integrity by preventing accidental modifications to entities that should remain unchanged.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html