Hibernate is an open-source o/R Mapping (Object link ing framework). It implements lightweight object encapsulation for JDBC, so that Java programmers can use the object programming thinking to manipulate the database.
Hibernate synchronizer is an Eclipse plug-in that can automatically generate *. HBM files, persistence classes, and daos. For more information about the hibernate synchronizer plug-in, visit: http://www.binamics.com/hibernatesync.
I. Functions of hibernate synchronizer include:
L configure and generate the hibernate configuration file through a wizard;
L use a wizard to synchronously generate the *. HBM file of the database table;
L use the *. HBM file to synchronously generate Hibernate Persistence classes and daos;
L provides the hibernate synchronizer editor to edit *. HBM files;
L use a language called velocity to customize personalized code and resource generation templates (usually unnecessary );
2. Steps for using the hibernate synchronizer plug-in:
1. Install the plug-in through the eclipse software update function (eclipse 3. x plug-in update URL: http://www.binamics.com/hibernatesync );
2. Configure the attributes of the plug-in. It can be configured at the workbench level and project level respectively. The default value is usually used. Configurable items include:
L whether to automatically update the value object when modifying and saving HBM );
L whether to automatically generate a daos or value object );
L package name when daos and value object are generated.
3. Generate the hibernate configuration file through the plug-in;
4. Generate the *. HBM (hibernate Mapping File) file through the plug-in;
5. Use the editor provided by the plug-in to edit *. HBM files if necessary;
6. reconfigure the hibernate ing resource of the hibernate configuration file through the plug-in;
7. Synchronously generate a daos and value object (Value Object) through *. HBM );
Iii. Analyze the code generated by the hibernate synchronizer plug-in.
In this example, The Hibernate synchronizer plug-in is used to synchronize two tables in the database: t_order and t_user. T_user and t_order are one-to-many relationships.
Under the default settings, the package diagram generated by the plug-in is as follows:
The base package contains the five abstract classes generated by the plug-in. The classes in the base package are overwritten when hibernate synchronizer is "re-synced". Therefore, you do not need to place the customer code in the class in the base. In other words, do not modify these classes at any time.
The three classes in the DaO package are inherited from the three corresponding classes in the base package. The three classes in the DaO package are completely empty. You can insert your own code here. With this structure, the customer code is separated from the code generated by the plug-in, which enables the customer to customize the code generated by the plug-in, it does not affect the Customer Code when the plug-in is "synchronized.
By default, the class diagram generated by the plug-in is as follows:
The six classes on the left are daos, and the four classes on the right are persistent classes.
All daos inherit from _ baserootdao. This abstract class encapsulates common hibernate operation methods. Abstract class _ rootdao directly inherits from _ baserootdao. Here, you can customize _ baserootdao and put common hibernate operations here. _ Baserootdao: base... Dao is the DAO of the persistent class, and it is still an abstract class, with the aim of making its subclass implement a custom method.
All value objects are divided into two levels: abstract base classes (such as basetorder and basetuser) and implementation classes (torder and Tuser ). The abstraction level contains the persistent class generated by the hibernate synchronizer plug-in, and the implementation level can contain the custom code of the customer.
4. Use the code generated by the hibernate synchronizer plug-in.
The code generated using hibernate Synchronizer in your own code is very simple.
_ Rootdao. initialize ();
Tuserdao Dao = new userdao ();
Tuser user = Dao. Load ("zhangyu ");
User. setmail (e-zhangyu@vip.sina.com );
Dao. Update (User );
_ Rootdao. initialize () is required. The persistent object generated by hibernate synchronizer is a standard hibernate Persistent object, which contains a set and get methods. Daos is responsible for operating persistent objects, including session and transaction management, load and release objects, save or update, query, and other functions. With daos, the customer code is very concise.