×
☰ See All Chapters

@ Table Annotation in JPA

Table annotation is used to map the entity class to the database table.  If you omit the Table annotation, base entity classes (Super class of entity class if entity class extends any other class) default to a table with their unqualified class name.

Tables have the following properties:

  1. String name: The name of the table. Defaults to the unqualified entity class name. 

  2. String schema: The table's schema. If you do not name a schema, JPA uses the default schema for the database connection. 

  3. String catalog: The table's catalog. If you do not name a catalog, JPA uses the default catalog for the database connection. 

  4. UniqueConstraint[] uniqueConstraints: An array of unique constraints to place on the table.  

package com.java4coding;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

 

@Entity

@Table(name = "STUDENT")

public class Student {

        @Id

        @Column(name = "id")

        private int id;

 

        @Column(name = "fistName")

        private String firstName;

 

        @Column(name = "lastName")

        private String lastName;

 

        @Column(name = "marks")

        private int marks;

       

        public Student() {

               

        }

 

        public Student(int id, String firstName, String lastName, int marks) {

                super();

                this.id = id;

                this.firstName = firstName;

                this.lastName = lastName;

                this.marks = marks;

        }

 

        public int getId() {

                return id;

        }

 

        public void setId(int id) {

                this.id = id;

        }

 

        public String getFirstName() {

                return firstName;

        }

 

        public void setFirstName(String firstName) {

                this.firstName = firstName;

        }

 

        public String getLastName() {

                return lastName;

        }

 

        public void setLastName(String lastName) {

                this.lastName = lastName;

        }

 

        public int getMarks() {

                return marks;

        }

 

        public void setMarks(int marks) {

                this.marks = marks;

        }

}

 


All Chapters
Author