First Hibernate Application

Steps to create Hibernate Application

  1. First create persistent class or beans class.
  2. create mapping file.
  3. create configuration file with configuration.cfg.xml extension
  4. store the persistent object
  5. load the hibernate jar file.
  6. run hibernate application
  1. First create persistent class or beans class.
  • it is simple class which does not have any main method. It have all field should be private.
  • it have constructor default or perametrized.
  • all field should have getter methods.
  • all field should have setter methods.
Example of persistent class
package beans;

public class Employee {
private int id;
private String name;
private String Add;
private String shift;
private String timing;
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 getAdd() {
return Add;
}
public void setAdd(String add) {
Add = add;
}
public String getShift() {
return shift;
}
public void setShift(String shift) {
this.shift = shift;
}
public String getTiming() {
return timing;
}
public void setTiming(String timing) {
this.timing = timing;
}
}
2-create a mapping file.
Mapping file name class_name.hbm.xml should be.

hibernate-mapping:it is root element of mapping file. it shows mapping is started.

id:Every table have one primary key column so we require a id.That's why we require id for mandatory for table.it is sub element of class.

class:it is class name of our persistent object. it is sub element of hibernate-mapping.

table name: it is require a table name where we store data.

Generator :it is sub element of id.it have many way to help making id as a primary key.

Property:it is fields of persistent class and column name of our database table.
employee.hbm.xml file.

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
 <hibernate-mapping>
 <class name="beans.Employee" table="emp001">
 <id name="id">
 <generator class="assigned"></generator>
 </id>
 <property name="name"></property>
<property name="add"></property>
 <property name="shift"></property>
 <property name="timing"></property>
   </class>
 
 </hibernate-mapping>


  1. create configuration file:
hibernate-configuration:
it is main root tag of configuration file it is shows that configuration file .
session-factory: it is sub root tag of hibernate-configuration .it also have many other root .basically this root give information about our database connection,database url,database username,database password,database dialect, database hbm2ddl.auto ,etc.

mapping resource:it is sub element of session factory .it give resources to our mapping file.

Example:
<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
 
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
<property name="connection.url">jdbc:mysql://localhost:3306:db</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">lk@9616918397y</property>  
        <property name="connection.driver_class">com.mysql.jdbc.driver.Driver</property>  
    <mapping resource="reources/employee.hbm.xml"/>  


</session-factory>
</hibernate-configuration>




  1. store the persistent object
package test;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import beans.Employee;

public class StoreData {

public static void main(String[] args) {
Configuration configuration=new Configuration();
configuration.configure("resources/configuration.cfg.xml");
SessionFactory sf=configuration.buildSessionFactory();
Session session=sf.openSession();
Transaction transaction=session.beginTransaction();
Employee e=new Employee();
e.setId(1);
e.setName("yadav");
e.setShift("morning");
e.setTiming("9:am");
session.persist(e);
transaction.commit();
session.close();
System.out.println("data success");
}

}

No comments:

Post a Comment