Home » Java » Hibernate automatic table building and some relate...

Hibernate automatic table building and some related technical summary

LZ recently downloaded from the GitHub a Struts2 + hibernate in the J2EE project, which used hibernate to operate the database, then I again on the hibernate operation of the database tools. First, hibernate can be configured through the ***.hbm.xml file to automatically generate reflection configuration related data table. Then write for each table of the Java class, and generate the corresponding ***.hbm.xml file to configure. It is important to note here, also need a key java to call the ***.hbm.xml file to generate the corresponding data table, one of the key issues there is a need to pay attention to the local hibernate configuration file is. The configuration of the update/create configuration directory is Name= "hibernate.hbm2ddl.auto" >update, this configuration can have four, respectively is the file update/create/create-drop/validate, which we use is update, this time said in the update, can not re create the table, and then automatically return to the table before and according to the comparison to update the new form. The next step is specific the call to ***.hbm.xml java code.


public class create
{
Static Session session;
Static, Configuration, config = null;
Static, Transaction, TX = null;
Public, static, void, main (String, args[])
{
Try
{
Config = new, Configuration ().Configure (new, File ("src/hibernate.cfg.xml"));
SessionFactory sessionFactory = config.buildSessionFactory ();
Session = sessionFactory.openSession ();
TX = session.beginTransaction ();
SchemaUpdate schemeUpdate = new SchemaUpdate (config);
SchemeUpdate.execute (true, true);
Tx.commit ();
}
Catch (HibernateException, ex)
{
Ex.printStackTrace ();
}
}
}


There is a SchemaUpdate class, the code in this class is used according to the configuration file to update, of course there is a SchemaExport class, this class has a method is schemaExport.create (true, true) method, this method is time when the run Java method, it will re create a new table so before will save the data into the database to cover, but not on the basis of the original re fill.



copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted

Latest