What is Hibernate ?
Hibernate is an Object Relational Mapping(ORM) . Hibernate is an open source tool which comes under GNU Lesser General Public License (LGPL) . Hibernate allow a plain java object mapping to relational database .
Hibernate persistent framework created by Gavin King in 2001.
How to persist an plain java object(POJO) to relational database .
Step 1 . Create Hibernate.cfg.xml configuration file .
Step 2 . Create Persistent java class .
Step 3 . Create Main class to create session and create java object . and persist class .
Hibernate.cfg.xml : contain various configuration property like database driver name ,database url
user name and password and all mapping class .
<property name="hibernate.dialect">org.hibernate.dialect.ProgressDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<!-- Assume hibernate is the database name -->
</hibernate-configuration>
Hibernate is an Object Relational Mapping(ORM) . Hibernate is an open source tool which comes under GNU Lesser General Public License (LGPL) . Hibernate allow a plain java object mapping to relational database .
Hibernate persistent framework created by Gavin King in 2001.
How to persist an plain java object(POJO) to relational database .
Step 1 . Create Hibernate.cfg.xml configuration file .
Step 2 . Create Persistent java class .
Step 3 . Create Main class to create session and create java object . and persist class .
Hibernate.cfg.xml : contain various configuration property like database driver name ,database url
user name and password and all mapping class .
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hbm2ddl.auto">update</property><property name="hibernate.dialect">org.hibernate.dialect.ProgressDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<!-- Assume hibernate is the database name -->
<property name="hibernate.connection.url">jdbc:postgresql://localhost/hibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<mapping class="Employee" />
</session-factory>
</hibernate-configuration>
Step 2 Create persistent java Class :
to Create java persistent class just create simple plain java class and decorated minimum two annotation
@Entity for mark java class hibernate entity and @Id to mark field for primary key .
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private int empId;
private String emapName;
private String companyName;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmapName() {
return emapName;
}
public void setEmapName(String emapName) {
this.emapName = emapName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
Step Create a main class to persist class : To persistent a java object with hibernate we have require Configuration object , SessionFactory object and session object .
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateTest {
public static void main(String[] args) {
Configuration configuration = new Configuration(); //
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Employee employee = new Employee();
employee.setEmpId(1);
employee.setCompanyName("Johny kumar");
employee.setCompanyName("Google.com");
Employee employee2 = new Employee();
employee2.setEmpId(2);
employee2.setCompanyName("Ambrish");
employee2.setCompanyName("Amazon");
session.persist(employee);
session.persist(employee2);
transaction.commit();
session.flush();
session.close();
System.out.println("Saved.............");
}
.
Your post about technology was very helpful to me. Very clear step-by-step instructions.
ReplyDeleteSpring Training in Chennai
Struts Course in Chennai
Hibernate Course in Chennai
Such an exceptionally valuable article. Extremely intriguing to peruse this article. I might want to thank you for the endeavors you had made for composing this amazing article.
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course |
CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course