Incremental schema updates through the hibernate
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() { System.out.println("initialization"); 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. */ configuration.configure(this.getClass().getClassLoader().getResource("com.breeze.shayanth.test/hibernate.cfg.xml")); /** * 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 */ configuration.addClass(com.breeze.shayanth.test.ACHI.class); configuration.addClass(com.breeze.shayanth.test.IGatewayProfileSelections.class); 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) { e.printStackTrace(); } }
Following is the hibernate.cfg.xml file. which have all the hibernate related resources.
com.mysql.jdbc.Driver root jdbc:mysql://127.0.0.1/DB_NAME org.hibernate.dialect.MySQLDialect
Now run the Javaclass, and enjoy the feasibility in the hibernate feature..
This Articles was posted on 10:17 PMThursday, July 23, 2009
|
Labels:
Hibernate,
Java,
Payment Gateway,
Project work
|
0 comments:
Post a Comment