JPA Entity Example | Java Persistence API

By Arvind Rai, September 06, 2013
JPA Entity is a java class referring to database table. It contains setter and getter method to save and fetch data from database. JPA provides annotation to correlate plain java class to a table and its attributes to table columns. The package of javax.persistence contains all the annotations to create an entity. Some basic annotation will be discussed below.


@Entity is the javax.persistence.Entity in java persistence API. A java class becomes JPA entity only when the class is annotated with @Entity.


@Table is javax.persistence.Table which specifies the table name associated with the given Entity. Entity name and column name can be different but if @Table has not been provided to entity, by default entity will associate the table with the same name as entity.


@Column is javax.persistence.Column. @Column is used to associate a column and an entity attribute.


@Id is javax.persistence.Id. @Id enables an attribute of an entity to behave like a primary key of a table.

Sample Entity is given below.
package com.concretepage.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
public class Farmer {
	private int id;
	private String name;
	private String village;
	public int getId() {
		return id;
	public void setId(int id) { = id;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getVillage() {
		return village;
	public void setVillage(String village) {
		this.village = village;

Download the source code which is a demo for end to end use of entity with a JPA application.

Download Source Code

©2023 | Privacy Policy | Contact Us