×
☰ See All Chapters

@Entity Annotation in JPA

The Entity annotation makes the class an entity class. All entity classes must have this annotation.  A class annotated with @Entity annotation will have a distinct separate existence. We can run DB queries, without being dependent on any other class. Each persistent instance of an entity class (each entity) represents a unique database record. You can use EntityManager to find an entity by its persistent identity or use a Query to find entities matching certain criteria. The Entity annotation takes one optional property, if it is not specified, the class name itself is considered as default value. This optional property takes string value which is a name used to refer the entity class in queries. Must not be a reserved literal in JPQL.

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