SchemaExport

SchemaExport

SchemaExport tool is provided by Hibernate software in the form of org.hibernate.tool.hbm2ddl.SchemaExport predefined class in hibernate3.jar file.

If we activate this tool then it will perform the following actions.

  • First SchemaExport tool will take name and location of hibernate configuration file which we have provided as input parameter.
  • SchemaExport tool will load and parse hibernate configuration file.
  • SchemaExport tool will recognize mapping file configuration in hibernate configuration file then it will load and parse mapping file.
  • As per mapping file details, SchemaExport tool will check whether any table is existed or not in Database.
  • If no table is existed then SchemaExport tool will create new table as per mapping file details.
  • If any table is existed with the name then SChemaExport tool will drop that existed table and creates new table as per mapping file configuration details.

To activate SchemaExport tool manually through command prompt then we have to use the following command on command prompt.

java org.hibernate.tool.hbm2ddl.SchemaExport --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 SchemaExport tool.

E.g

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/hibernate.hbm.xml"/>
   </session-factory>
</hibernate-configuration>

Command on Command prompt

javac Employee.java
java org.hibernate.tool.hbm2ddl.SchemaExport --config = hibernate.cfg.xml

SchemaExport


Scroll to top