Youtube Channel

hbm2ddl.auto

  • Is one of the hibernate property used to perform operations on database schema.
  • Operations or values of hbm2ddl.auto are—
  1. Create
  2. Create-drop
  3. Update
  4. Validate
Lets we have a pojo class named book with properties id,name,amt now we will apply the above operations on its configuration file so, that we will not need to create the database table. Previously we need to manually create the table.
book
book_hbm_xml
bookmain_java

  • Create:                 It is always used to create a new schema or table .if table exists then it will be replaced with the new table.
To use it add the following code in configuration file:
<Property name=”hibernate.hbm2ddl.auto”>create</property>
hibernate_cfg_xml
  • Create-drop: Every time it creates a new schema and removes schema after closing the session.
To use it add the following code in configuration file:
<Property name=”hibernate.hbm2ddl.auto”>create-drop</property>
hibernate_cfg_xml_create_drop
  • Update:  it is used to update the schema or table e.g. adding columns
To use it add the following code in configuration file:
<Property name=”hibernate.hbm2ddl.auto”>update</property>
hibernate_cfg_xml_update
  • Validate: It always verifies columns name, types etc.
To use it add the following code in configuration file:
<Property name=”hibernate.hbm2ddl.auto”>validate</property>
 hibernate_cfg_xml_validate
Points to note:
  • Update will not remove existing data where create will remove existed data.
  • Validate will always cross-verifies pojo design with database schema. e.g. type checking, naming conflictions, integrity constraints etc.

0 comments: