Developing Hibernate applications with Eclipse Hibernate Synchronizer Plug-ins

Source: Internet
Author: User
Tags abstract contains functions include resource client

Hibernate is an open source O/R Mapping (Object Relational Mapping framework) that gives JDBC a lightweight object encapsulation that allows Java programmers to manipulate the database using object-programming thinking.

Hibernate Synchronizer is an Eclipse plug-in that automatically generates *.HBM files, persistent classes, and Daos. For more information about Hibernate synchronizer Plug-ins, you can access: http://www.binamics.com/hibernatesync/.









The functions of Hibernate Synchronizer include:

L Configure and generate hibernate Configuration File with a wizard;

L Synchronize the *.HBM file of the database table with a wizard;

L Generate Hibernate persistence classes and Daos through *.HBM files synchronously;

L provide Hibernate Synchronizer editor edit *.HBM documents;

• Customizing personalized code and resource generation templates (usually not necessary) in a language called velocity;









Second, the use of Hibernate synchronizer plug-in steps:

1. Install Plug-ins via Eclipse's Software update feature (Eclipse 3.X plugin update URL:: Http://www.binamics.com/hibernatesync);

2, configure the properties of Plug-ins. Can be configured at the workbench level and at the project level, respectively. Default values are usually used. Configurable items include:

L Update the Value object automatically when HBM is modified and saved;

L whether to automatically generate Daos or value objects (values object);

L configures the name of the package when generating the Daos and value objects (values object).

3, through Plug-ins to generate Hibernate Configuration File;

4, through Plug-ins to generate *.HBM (Hibernate Mapping file) files;

5, if necessary, with Plug-ins to provide editor edit *.HBM file;

6, through the plug-in to reconfigure Hibernate Configuration file mapping resource;

7, through *.HBM synchronous generation of Daos and Value objects (value object);









Third, analysis of hibernate synchronizer Plug-ins generated code.

In this example, the two tables in the database are synchronized using the Hibernate Synchronizer plug-in: T_order and T_user. T_user and T_order are a pair of many relationships.

In the default settings, the plug-in generates the following package diagram:

















The five abstract classes that are generated by the plug-in are stored in the base package, overwriting the classes in the base package when hibernate synchronizer "resynchronization", so that the user does not place the client code in the class in base. In other words, do not modify these classes at any time.

The three classes stored in the DAO package, inherited from the corresponding three classes in the base package, respectively. The three classes in the DAO package are completely empty implementations where customers can insert their own code. With such a structure, the client code is separated from the code generated by the plug-in, that is, the implementation of custom plug-in generation code, and will not affect the plug-in "resynchronization" when the customer code.

With the default settings, 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, and this abstract class encapsulates the common Hibernate action method. Abstract class _rootdao directly inherits from _baserootdao, where customers can customize the _baserootdao and place the common-level hibernate operations here. _baserootdao Subordinate base ... DAO is the DAO of the persistent class, it is still an abstract class, the purpose is to let its subclasses implement the custom method.

All Values objects (value object) are divided into two levels, abstract base classes (such as Basetorder, Basetuser), and implementation classes (Torder, TUser). The abstraction hierarchy contains the persistent classes generated by the Hibernate Synchronizer plug-in, and the implementation hierarchy can contain custom code for the customer.









Iv. code generated using the Hibernate Synchronizer plug-in.

The code generated by 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 a must. The persistent object generated by Hibernate Synchronizer is a standard Hibernate persistent object that contains a set of set and get methods. Daos is responsible for the operation of persistent objects, including functions such as session and transaction management, load and release objects, save or update, queries, and so on. With the Daos, the customer code is very concise.









Author Introduction:

Yu Zhang, you can get in touch with the author through e-zhangyu@vip.sina.com.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.