@Table - JPA Annotation

 @Table JPA annotation specifies the table in the database with which this entity is mapped.

JPA @Table Annotation Example

In most cases, the name of the table in the database and the name of the entity will not be the same.

In these cases, we can specify the table name using the @Table annotation:

In the below code snippet, we are specifying student table name in the database.
@Entity
@Table(name = "student")
public class Student {
}

We can also mention the schema using the schema element:

@Entity
@Table(name="student", schema="college")
public class Student {
    // fields, getters and setters
}

Schema name helps to distinguish one set of tables from another,

If we do not use the @Table annotation, the name of the entity will be considered the name of the table.

References

https://javaee.github.io/javaee-spec/javadocs/javax/persistence/Table.html.

Comments