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)
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 + "]";
}
}
@Entity Annotation
@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
@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
@Entity @Table(name = "student") public class Student { @Id private int id; }
@GeneratedValue
@Entity @Table(name = "student") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; }
@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 }
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.
Comments
Post a Comment
Leave Comment