Hibernate is an O / R Mapping of an open source code that performs a lightweight object package for JDBC, so that Java programmers can use object programming to manipulate the database.
Hibernate Synchronizer is an Eclipse plugin that automatically generates * .hbm files, persistence classes, and DAOS. Understand the details of the Hibernate Synchronizer plugin Access: http://www.binamics.com/hibernateSync/.
First, the features of Hibernate Synchronizer include:
l Configure and generate Hibernate Configuration File via a wizard;
l Synchronize the * .hbm file of the database table through a wizard;
l Generate Hibernate persistence classes and DAOS by * .hbm file;
l Provide Hibernate Synchronizer Editor editing * .hbm file;
l Customize individualized code and resource generation templates with a language called Velocity (usually there is no need);
Second, the step of using the Hibernate Synchronizer plugin:
1. Install the plugin (Eclipse 3.x plugin Update URL :: http://www.binamics.com/Hibernatesync);
2, configure the properties of the plugin. It can be configured on the table level and project level, respectively. The default value is usually used. Configurable items include:
l Modify and save the value object when the HBM is saved; Value Object;
l Whether the DAOS or value object is automatically generated;
l Configure the package name when Generating a DAOS and Value Object.
3, generate hibernate configuration file by plugin;
4, generate * .hbm (hibernate mapping file) by plugin;
5. Editor file provided by the plugin is required if necessary; HHBM file;
6. Reconfigure the mapping resource of Hibernate Configuration File by plugin;
7. Synchronize the DAOS and value objects by * .hbm;
Third, analyzes the code generated by the Hibernate Synchronizer plugin.
In this example, two tables in the database are synchronized using the Hibernate Synchronizer plugin: t_order and t_user. T_user and t_order are a pair of relationships.
In the case of default settings, the package map generated by the plugin is as follows:
The five abstract classes generated in the Base package are stored in the Hibernate Synchronizer "Synchronize", so users do not put the customer code in the class in Base. In other words, don't modify these classes at any time.
Three classes stored in the DAO package, inherit three classes corresponding to the Base package. The three classes in the DAO package are completely empty, and customers can insert their own code here. With such a structure, the customer code is separated from the code generated by the plugin, that is, the customer's customization of the plug-in generates code is realized, and it will not affect the customer code when the plugin "resynchronize".
In the case of default settings, the class diagrams generated by the plugin are as follows:
The six classes on the left are DAOS, and the four classes on the right are lasting classes. All DAOS inheritance from _BaseRootdao, this abstract class encapsulates the public hibernate operation method. Abstract class _rootdao directly inherited from _BaseRootdao, customers can customize _baserootdao here, putting public level Hibernate operations here. _BaseRootdao's subordinate base ... DAO is a lasting DAO, which is still an abstract class that is a way to make it a custom method.
All values objects are divided into two levels, abstract base classes (such as BasetOrder, BaseTuser) and implementation classes (Torder, Tuser). Abstract hierarchy contains the persistent class generated by the Hibernate Synchronizer plugin, and implements hierarchical codes.
Fourth, use the code generated by the Hibernate Synchronizer plugin.
The code generated using Hibernate Synchronizer in its own code is very simple.
_Rootdao.initialize ();
TUSERDAO DAO = New Userdao ();
Tuser = dao.load ("zhangyu");
User.setmail ("e-zhangyu@vip.sina.com");
Dao.update (user);
_Rootdao.initialize () is required. Hibernate Synchronizer generated persistent objects are standard Hibernate persistent objects that contain a set of sets and get methods. DAOS is responsible for operating a persistent object, including session and transaction management, LOAD, and release objects, Save or Update, query and other functions. With DAOS, the customer code is very simple.
About the Author:
Zhang Wei, you can contact the author through e-zhangyu@vip.sina.com.