SchemaUpdate
SchemaUpdate tool is provided by Hibernate software in the form of org. hibernate.tool.hbm2ddl.SchemaUpdate predefined class in hibernate3.jar file.
If we activate this tool then it will perform the following actions.
- First SchemaUpdate tool will take name and location of hibernate configuration file which we have provided as input parameter.
- SchemaUpdate tool will load and parse hibernate configuration file.
- SchemaUpdate tool will recognize mapping file configuration in hibernate configuration file then it will load and parse mapping file.
- As per mapping file details, SchemaUpdate tool will check whether any table is existed or not in Database with the same name.
- If no table is existed then SchemaUpdate tool will create new table as per mapping file details.
- If any table is existed with the name then SchemaUpdate tool will alter that existed table as per mapping file configuration details.
Note
When mapping contains new columns details when compared with database table then only SchemaUpdate tool will perform alter operation. If Database table contains more columns when compared with mapping file configuration details then SchemaUpdate tool will not perform alter operation.
To activate SchemaUpdate tool manually through command prompt then we have to use the following command on command prompt.
java org.hibernate.tool.hbm2ddl.SchemaUpdate --config =hibernate.cfg.xml
Note
To run the above command we must copy all hibernate jars to “C:\Java\jdk1.7.0\jre\lib\ext” folder and we must provide POJO class, mapping file and configuration file at the same location from where we are activating SchemaUpdate tool.
package com.ashok.hibernate.sample.model; /** * * @author ashok.mariyala * */ public class Employee { private int id; private String empName; private String address; private double salary; public Employee(String empName, String address, double salary) { super(); this.empName = empName; this.address = address; this.salary = salary; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
employee.hbm.xml
<?xml version = "1.0" encoding = "utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.ashok.hibernate.sample.model.Employee" table="emp"> <id name="id" type="int" column="id"> <generator class="native" /> </id> <property name="empName" column="emp_name" type="string" /> <property name="address" column="address" type="string" /> <property name="salary" column="salary" type="double" /> </class> </hibernate-mapping>
hibernate.cfg.xml
<?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.dialect">org.hibernate.dialect.OracleDialect</property> <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="hibernate.connection.username">system</property> <property name="hibernate.connection.password">ashok</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/ashok/hibernate/sample/Account.hbm.xml"/> </session-factory> </hibernate-configuration>
Command on Command prompt
javac Customer.java java org.hibernate.tool.hbm2ddl.SchemaExport --config =hibernate.cfg.xml