Home  >  JPA

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

@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

@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

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

@Id

@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.

Farmer.java
package com.concretepage.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="farmer")
public class Farmer {
	@Id
	@Column(name="id")
	private int id;
	@Column(name="name")
	private String name;
	@Column(name="village")
	
	private String village;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.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
jpa-entity-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us