Java MCQ: Which JPA annotation is used to define the table name for an entity?
a) @Entity
b) @Table
c) @Column
d) @PersistenceTable
Answer:
b) @Table
Explanation:
The @Table
annotation in JPA is used to define the table name for an entity. This annotation is applied at the class level and specifies the name of the database table to which the entity will be mapped. If the @Table
annotation is not specified, JPA uses the class name as the default table name.
Here’s an example:
@Entity
@Table(name = "employees")
public class Employee {
@Id
private Long id;
private String name;
// Getters and setters
}
In this example, the Employee
entity is mapped to the employees
table in the database using the @Table(name = "employees")
annotation.
The @Table
annotation is useful for customizing the mapping between an entity and its corresponding database table, especially when the table name differs from the class name.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html