Java MCQ: What is the purpose of the @Transient annotation in JPA?
Answer:
Explanation:
The @Transient
annotation in JPA is used to exclude a field from persistence. When a field is annotated with @Transient
, it is not mapped to any database column and is ignored by the JPA provider during the persistence process. This annotation is useful for fields that are used for temporary calculations or logic that should not be stored in the database.
Here’s an example:
@Entity
public class Employee {
@Id
private Long id;
private String name;
@Transient
private int tempValue;
// Getters and setters
}
In this example, the tempValue
field is marked with the @Transient
annotation, meaning that it will not be persisted in the database when the Employee
entity is saved.
The @Transient
annotation allows developers to keep certain fields in their entities without persisting them, providing flexibility in the design of entity classes.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html