This is basic example of hibernate configuration which contain information about how interact with database using hibernate framework.
Example of Hibernate Configuration
PROJECT STRUCTURE
Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.kodemaker.hibernate; import java.io.Serializable; public class Employee implements Serializable{ private int employeeId; private String employeeName; private String employeeSalary; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmployeeSalary() { return employeeSalary; } public void setEmployeeSalary(String employeeSalary) { this.employeeSalary = employeeSalary; } } |
Employee.hbm.xml
This xml conatain information about how object store into database.This example show how Employee object map into table.
increment: here primary key is employeeId which uses generator class is increment it means each time when new record insert into database primary key is incremented by 1 for new record.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> |
Client.java
SessionFactory:-SessionFactory is available for particular database if we want to use another database we have to create new SessionFactory. and it is a thread safe.
Session:-It provide physical connection with database. and it is not thread safe.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.kodemaker.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Client { private static SessionFactory sessionFactory; public static void main(String[] args) { sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); Employee employee=new Employee(); employee.setEmployeeName("John"); employee.setEmployeeSalary("100000"); session.save(employee); session.getTransaction().commit(); } } |
hibernate.cfg.xml
This is configuration file which conatain information about database property like driver class , url ,username, password.
show_sql: It show query which is generated by hibernate.
Dialect is specific for different database.it provide support to hibernate to how interact with database specific language.
hbm2ddl.auto: It validate the DDL when database schema is created.
possible value of this property is
- validate-validate schema.
- update-update schema.
- create-create schema every time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/kodemaker/hibernate/Employee.hbm.xml"/> </session-factory> </hibernate-configuration> |
No comments:
Post a Comment