Defining JPA Entity - @Entity Annotation

In this tutorial, we'll learn how to define a basic JPA entity with an example.

Learn Hibernate at https://www.javaguides.net/p/hibernate-tutorial.html.

Learn JPA at https://www.javaguides.net/p/jpa-tutorial-java-persistence-api.html.

What is JPA Entity?

Entities in JPA are nothing but POJOs representing data that can be persisted in the database. An entity represents a table stored in a database. Every instance of an entity represents a row in the table.

@Entity - JPA Annotation Example

Creating the JPA Entity Class(Persistent class)

Let's create a Student persistent class that is mapped to a student database table:
Let's create a Student entity class with @Entity annotation:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    public Student() {

    }

    public Student(String firstName, String lastName, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    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 String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }
}
Let's understand different JPA annotations from the above JPA entity.

@Entity Annotation

This annotation specifies that the class is an entity:
@Entity
public class Student {
}

The entity name defaults to the name of the class. We can change its name using the name element.

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

@Table

This annotation specifies the table in the database with which this entity is mapped.

Here the student is the 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.

@Id

This annotation specifies the primary key of the entity:
@Entity
@Table(name = "student")
public class Student {

    @Id
    private int id;
}
Each JPA entity must have a primary key that uniquely identifies it. The @Id annotation defines the primary key. 

@GeneratedValue

This annotation specifies the generation strategies for the values of primary keys:
@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
}
We can generate the identifiers in different ways which are specified by the @GeneratedValue annotation.

We can choose from four id generation strategies with the strategy element. The value can be AUTO, TABLE, SEQUENCE, or IDENTITY.

@Column

The @Column annotation is used to specify the mapping between a basic entity attribute and the database table column.:
@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "first_name", length=50, nullable=false, unique=false)
    private String firstName;

    // getters and setters
}
The @Column annotation has many elements such as name, length, nullable, and unique.

The name element specifies the name of the column in the table. 
The length element specifies its length. 
The nullable element specifies whether the column is nullable or not, and the unique element specifies whether the column is unique.

If we don't specify this annotation, the name of the field will be considered the name of the column in the table.

Rules or Requirements to define Entity Class


The Entity Class of the JPA 2.1 specification defines its requirements for an entity class. Applications that wish to remain portable across JPA providers should adhere to these requirements.

  • The entity class must be annotated with the javax.persistence.Entity annotation (or be denoted as such in XML mapping)
  • The entity class must have a public or protected no-argument constructor. It may define additional constructors as well.
  • The entity class must be a top-level class.
  • An enum or interface may not be designated as an entity.
  • The entity class must not be final. No methods or persistent instance variables of the entity class may be final.
  • If an entity instance is to be used remotely as a detached object, the entity class must implement the Serializable interface.
  • Both abstract and concrete classes can be entities. Entities may extend non-entity classes as well as entity classes, and non-entity classes may extend entity classes.
  • The persistent state of an entity is represented by instance variables, which may correspond to JavaBean-style properties. An instance variable must be directly accessed only from within the methods of the entity by the entity instance itself. The state of the entity is available to clients only through the entity’s accessor methods (getter/setter methods) or other business methods.

Related JPA Tutorials

Comments