Best view in Firefox and Chrome

About the Author

SubanuHi all.. Shayanthan (alias Subanu) is an Engineering student from University of Moratuwa, Sri Lanka, in the Department of Computer Science and Engineering more »

     Email me       Linkedin

Breeze on Facebook

Incremental schema updates through the hibernate

Thursday, July 23, 2009
Convert Article to PDFPrint ArticleEmail Article to FrinedBookmark this Article

Here I'm going to explain how we can add/update new schema to the existing database schema through the hibernate. How we can do the Incremental schema updates through the hibernate.

You may all know that one of Hibernate's most useful features is the automatic generation of schema manipulation commands. This feature, sometimes referred to as the ability to generate Data Definition Language (DDL) scripts, makes it possible (given a valid *.hbm.xml file) to create, update, and even drop tables in a target database. You can do this at runtime, during development, or via scripts generated for later use by a system administrator—an invaluable capability if you expect to support multiple target databases (during either development or deployment) or have a high degree of database schema change. I'm going to use this feature to do the Incremental schema update in a specified database.

Here I like to introduce the SchemaUpdate tool (net.sf.hibernate.tool.hbm2ddl.SchemaUpdate) which can use to update an existing schema with "incremental" changes. But the problem is this SchemaUpdate depends upon the JDBC metadata API and, as such, will not work with all JDBC drivers. Here I'm using MySQL DB.

Lets Consider one simple example. Then you can easily understand the real usefulness of this tool. An application with a user object (and corresponding user table). You've decided to add a property to the user object to track the user's country code (previously the application only supported U.S. addresses). You make the change to your *.hbm.xml file and the corresponding Java code, and now would like to reflect the change in the deployed database. And also it support to create a new table corresponding to the object and the hbm.xml file. We can embedded the Schema Updates section within your application.

Lets start, Following is the Javacode to run the SchemaUpdate-

public void testSchemaUpdates() {
    try {      
 Configuration configuration = new Configuration(); 
 * Add the resources file which have the all the hibernate resources details.
 * I attached that file also. Please refer that also.

 * Now we need to add the class which we needs to reflect the changes in the database.      
 *  Here I add two classes ACHI, 
 *  IGatewayProfileSelections both are in the com.breeze.shayanth.test packege.     
 *  Please Note that hibernate.cfg.xml and the rest of the related *.hbm.xml files aslo need to put in the same dir      

 SchemaUpdate su = new SchemaUpdate(configuration);
 su.execute(true, true);

 /** You can use this tool also.      
 * new SchemaExport(configuration).execute(false, true, false, false);      
 } catch (Exception e) {

Following is the hibernate.cfg.xml file. which have all the hibernate related resources.



Now run the Javaclass, and enjoy the feasibility in the hibernate feature..

Read these Articles :


Why Breeze

This is a new technological rhythm in the web. It blooms like a Breeze in your technological face with more than 75 technoligical atricles and guidences.

Contact Me
Contact Me Send an Email Facebook Account LinkedIn profile Twitter/Shayanth
Share And Save
Social Share with Twitter Facebook Share Add to Google Save on Delicious
Get Updates

The RSS will facilitate you to get the updates from Breeze. Click Here to redirect to the RSS link.

Designed by Posicionamiento Web | Bloggerized by GosuBlogger | Blue Business Blogger | Customized by SHAUOM