CodeGeneration

CodeGeneration

The main intention of CodeGeneration tool is to create POJO class as per database table configurations which we provided in mapping file.

Note

This is available up to Hibernate 2.x version, but, it is deprecated in Hibernate3.x version.

In Hibernate applications, we are able to activate SchemaExport and SchemaUpdate tools in declarative manner also, for this, we have to use the following property in hibernate configuration file.

hibernate.hbm2ddl.auto

The above property will take the following three values.

  1. create
  2. update
  3. create-drop
Create

This option for “hibernate.hbm2ddl.auto” property will activate SchemaExport tool, it will create a table irrespective of the table is existed or not.

<?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">create</property>

      <mapping resource="com/ashok/hibernate/sample/employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
Update

This option for “hibernate.hbm2ddl.auto” property will activate SchemaUpdate tool, it will create table if table is not existed, it will alter the table if table is existed as per the mapping file.

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

This option for “hibernate.hbm2ddl.auto” will create table when SessionFactory object is created and it will drop table when SessionFactory object is closed.

<?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">create-drop</property>

      <mapping resource="com/ashok/hibernate/sample/employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
CodeGeneration
Scroll to top