Hibernate Entity

Ads
 
Tutorials   
What is difference between Hibernate 4 and 5? Hibernate 5.3 features Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORMwith Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse?

Hibernate Entity

This section contains the concept of hibernate Entity.

This section contains the concept of hibernate Entity.

Hibernate Entity

This section contains the concept of hibernate Entity.

Hibernate Entity: In basic terms an entity is a stand alone object that has some distinct value.
In Hibernate's persistent mechanism ,an entity is a business object, which can be managed freely by other objects.
Each entity has associated distinct table in relational database. Table row is represented by instance ot the entity.
Entity class is a simple annotated POJO.

Here are some points to be noted during writing entity class:

Class must have constructor

Create getter setter for each field

Don?t define any field static or final. 

Example:
Here is an persistence entity class named Employee.java.It mapped the employee table.

package net.roseindia.table;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "employee")
public class Employee implements Serializable {

public Employee() {

}

@Id
@GeneratedValue
@Column(name = "emp_id")
private int id;

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

@Column(name = "salary")
private int salary;

@Column(name = "date_of_join")
private Date dateOfJoin;

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 int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

public Date getDateOfJoin() {
return dateOfJoin;
}

public void setDateOfJoin(Date dateOfJoin) {
this.dateOfJoin = dateOfJoin;
}

}

Here is your main class -

package net.roseindia.main;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateEntityExp {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Employee emp = new Employee();
emp.setName("Ron");
emp.setSalary(20000);
emp.setDateOfJoin(new Date());
Transaction transaction = session.beginTransaction();
session.save(emp);
transaction.commit();
String hql = "SELECT emp.name, emp.salary, emp.dateOfJoin from Employee emp";
Query query = session.createQuery(hql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List objectList = query.list();
Iterator iterator = objectList.iterator();
System.out.println("Employee Name \t Salary \t Date of Join");
while (iterator.hasNext()) {
Map map = (Map) iterator.next();
System.out.print(map.get("0"));
System.out.print("\t\t" + map.get("1"));
System.out.print("\t\t" + map.get("2"));
System.out.println();
}

session.close();
}

}

Description : Here you are inserting new record by setting value.

session.save(emp) saves the emp object into your table.

By using HQL we are printing the table record.

Here is your output -

Hibernate: insert into employee (date_of_join, name, salary) values (?, ?, ?)
Hibernate: select employee0_.name as col_0_0_, employee0_.salary as col_1_0_, employee0_.date_of_join as col_2_0_ from employee employee0_
Employee Name  Salary   Date of Join
Mandy          20000    2010-03-12 00:00:00.0
Som            20000    1999-06-22 00:00:00.0
Mandy          15000    2003-12-23 00:00:00.0
Roxi           220000   2001-02-03 00:00:00.0
Rowdy          20000    2012-06-27 00:00:00.0
Ron            20000    2012-06-27 00:00:00.0

Ads

Ads