Hibernate – What is Entity ?

 

What is an Entity ?

An entity is something that has a distinct, separate existence. An entity could be viewed as a set containing subsets. In philosophy, such sets are said to be abstract objects. In simple language it is Plain Old Java objects without any business login inside it and mainly used as a Data carry object or value object with may represent the database tuple in form of an object.

 

Rules for Writing Entity
– it is POGO with annotations mapping :- >

1.      It must have default constructors.

2.      It should have getter/setters for all the fields.

3.      It should not contain static or final fields.

 

Sample Code for Entity

 

package com.premaseem.course;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Table;

 

 

@Entity

@Table(name=”COURSES”)

public class Course {

 

private long courseId;

private String courseName;

 

public Course() {

}

 

public Course(String courseName) {

this.courseName = courseName;

}

 

@Id

@GeneratedValue

@Column(name=”COURSE_ID”)

public long getCourseId() {

return this.courseId;

}

 

public void setCourseId(long courseId) {

this.courseId = courseId;

}

 

@Column(name=”COURSE_NAME”, nullable=false)

public String getCourseName() {

return this.courseName;

}

 

public void setCourseName(String courseName) {

this.courseName = courseName;

}

}

Entity explained with annotation:

As you can see we have used annotations in the code to do the object relational mapping. Hibernate supports EJB 3 persistence annotations. The EJB 3 annotations are contained in the javax.persistence package. If you avoid using Hibernate specific features in your application, then you will have the freedom to deploy your application in any environment that supports EJB 3. Anything imported from javax.persistence package tree is EJB 3 supported and anything imported from org.hibernate package tree is Hibernate specific.

The @Entity annotation is used to mark this class as an Entity bean. So the class should atleast have a package scope no-argument constructor.

The @Table annotation is used to specify the table to persist the data. The name attribute refers to the table name. If @Table annotation is not specified then Hibernate will by default use the class name as the table name.

The @Id annotation is used to specify the identifier property of the entity bean. The placement of the @Id annotation determines the default access strategy that Hibernate will use for the mapping. If the @Id annotation is placed over the field, then filed access will be used. Instead if it placed over the getter method of that field, then property access will be used. Here we use property access.

The @GeneratedValue annotation is used to specify the primary key generation strategy to use. If the strategy is not specified by default AUTO will be used.

The @Column annotation is used to specify the details of the column to which a field or property will be mapped. Here the courseId property is mapped to COURSE_ID column in the COURSES table. If the @Column annotation is not specified by default the property name will be used as the column name.

Entity states

  • New (transient): an entity is new if it has just been instantiated using the new operator, and it is not associated with a persistence context. It has no persistent representation in the database and no identifier value has been assigned.
  • Managed (persistent): a managed entity instance is an instance with a persistent identity that is currently associated with a persistence context.
  • Detached: the entity instance is an instance with a persistent identity that is no longer associated with a persistence context, usually because the persistence context was closed or the instance was evicted from the context.
  • Removed: a removed entity instance is an instance with a persistent identity, associated with a persistence context, but scheduled for removal from the database.

The EntityManager API allows you to change the state of an entity, or in other words, to load and store objects.

Prem Aseem

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s