Hbm2DLL.auto

The first thing that we should know about this Hibernate Configuration option is, never ever use that (Hbm2DLL.auto) option in production region. However, it can increase development speed in lower/dev regions, and that’s a good practice for large project that wanted to start from scratch – if they have a standard for database schema, and their methodology is Code-First development, but since there is no guarantee in data integrity for existed database schemas, it’s better and safer to avoid using this option in production region. So for legacy application, DON’T  use this option.

Validate: it won’t change the existed tables, but it validate and make sure the mapping file matches what we already have in database.

update: won’t drop, But it will add if the table is there/

create: create tables that are not in db, and drop the one which is already existed and create them again.

create-drop: like create option but it will drop all the tables it created when the SessionFactory closes.

This feature is useful only for development. Create your POJO classes, and then their mapping and that’s all. Then you can set this item in the hibernate.cfg.xml. Then go to database and export the DDL and put the DDL into your actual production environment.

<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">***********</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/MyDataBaseName</property>
  <property name="hibernate.connection.username">appuser</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.use_sql_comments">true</property>
  <property name="hibernate.hbm2ddl.auto">create</property>
  <property name="hibernate.format_sql">true</property>
  <mapping resource="com/navid/practice/hibernate/User.hbm.xml"/>
  <mapping resource="com/navid/practice/hibernate/Plan.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

Leave a comment