Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring Data JPA - @Table Annotation

Spring Data JPA provides a set of annotations to ease the mapping between Java objects and relational database tables. One of the fundamental annotations in this mapping is the @Table annotation. When you are working with JPA (Java Persistence API), the @Table annotation is used to specify the details of the table that will be used to persist the entity in the database.

Here's a breakdown of the @Table annotation and its attributes:

  1. name: Specifies the name of the table.

    @Entity
    @Table(name = "user_table")
    public class User {
        // fields, getters, setters...
    }
    

    In the example above, the User entity would be mapped to the user_table in the database.

  2. catalog: Specifies the name of the catalog. This is not often used because many databases don't use catalogs in the way JPA expects.

  3. schema: Specifies the name of the schema.

    @Entity
    @Table(name = "user_table", schema = "my_schema")
    public class User {
        // fields, getters, setters...
    }
    

    In this case, the User entity would be mapped to the user_table inside the my_schema schema in the database.

  4. uniqueConstraints: This is an array of @UniqueConstraint which allows you to specify which combinations of columns should be unique.

    @Entity
    @Table(name = "user_table", uniqueConstraints = {
        @UniqueConstraint(columnNames = {"email"})
    })
    public class User {
        // fields, getters, setters...
    }
    

    Here, the email column is required to have unique values in the user_table.

  5. indexes: Introduced in JPA 2.1, this allows you to specify indexes on your table.

    @Entity
    @Table(name = "user_table", indexes = {
        @Index(name = "index_email", columnList = "email")
    })
    public class User {
        // fields, getters, setters...
    }
    

    This specifies that an index named index_email should be created on the email column of the user_table.

By default, if you don't specify the table's name using @Table, the entity name (class name) will be used as the table name. However, it's a good practice to explicitly provide table names, especially if your database has naming constraints or if you want to follow a specific naming convention.

Lastly, remember to always pair the @Table annotation with the @Entity annotation, as the latter indicates that the class is a JPA entity.

  1. Using @Table annotation in Spring Data JPA:

    • The @Table annotation is used to customize the details of the table associated with a JPA entity.
    // User.java
    import javax.persistence.Entity;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "app_user")
    public class User {
    
        // Fields and methods...
    }
    
  2. @Table annotation properties in Spring Data JPA example:

    • Customize table properties such as name, schema, and catalog using the attributes of the @Table annotation.
    // Product.java
    import javax.persistence.Entity;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "products", schema = "inventory", catalog = "store")
    public class Product {
    
        // Fields and methods...
    }
    
  3. How to customize table names with @Table in Spring Data JPA:

    • Use the name attribute of the @Table annotation to specify a custom table name for the associated entity.
    // Order.java
    import javax.persistence.Entity;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "customer_orders")
    public class Order {
    
        // Fields and methods...
    }
    
  4. Defining schema and catalog with @Table annotation in Spring Data JPA:

    • Specify the database schema and catalog using the schema and catalog attributes of the @Table annotation.
    // Customer.java
    import javax.persistence.Entity;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "customers", schema = "crm", catalog = "company")
    public class Customer {
    
        // Fields and methods...
    }
    
  5. Using @Table with secondary tables in Spring Data JPA:

    • Employ the @SecondaryTable and @Table annotations to work with secondary tables associated with an entity.
    // Employee.java
    import javax.persistence.Entity;
    import javax.persistence.Table;
    import javax.persistence.SecondaryTable;
    
    @Entity
    @Table(name = "employees")
    @SecondaryTable(name = "employee_details", pkJoinColumns = @PrimaryKeyJoinColumn(name = "employee_id"))
    public class Employee {
    
        // Fields and methods...
    }
    
  6. Customizing indexes and constraints with @Table in Spring Data JPA:

    • Utilize the indexes and uniqueConstraints attributes of the @Table annotation to customize indexes and constraints.
    // Product.java
    import javax.persistence.Entity;
    import javax.persistence.Table;
    import javax.persistence.Index;
    import javax.persistence.UniqueConstraint;
    
    @Entity
    @Table(name = "products", indexes = @Index(columnList = "name"), uniqueConstraints = @UniqueConstraint(columnNames = "sku"))
    public class Product {
    
        // Fields and methods...
    }
    
  7. Mapping entities to database tables with @Table in Spring Data JPA:

    • Use the @Table annotation to map entities to specific database tables.
    // Book.java
    import javax.persistence.Entity;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "library_books")
    public class Book {
    
        // Fields and methods...
    }
    
  8. Table per class hierarchy with @Table in Spring Data JPA:

    • Implement table per class hierarchy using the @Table annotation when working with inheritance.
    // Vehicle.java
    import javax.persistence.Entity;
    import javax.persistence.Inheritance;
    import javax.persistence.InheritanceType;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "vehicles")
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    public class Vehicle {
    
        // Fields and methods...
    }