CodeWeblog.com » mapping framework,durable type,persistent classes » Hibernate code generation tools for the design of the entire Raiders -大口仔- Garden blog

Hibernate code generation tools for the design of the entire Raiders -大口仔- Garden blog

Points Writing essay and the latest rankings here for easy access the main Ha ha -大口仔-大口仔- qianq put my keyboard with a small knife out of Alice, wiping a bit sensitive keyboard you reply! - steward good - students read the list of software reviews reading list within 60 days ranking

1. Brief
Hibernate is an open source object-relational mapping framework, it had a lightweight JDBC object package, to provide HQL query language, Java programmers can make use of object programming way of thinking to manipulate the database. The use of Hibernate, mapping file must be configured-ClassMapping File and Configuration File, is now available on the market a lot of Hibernate code generation tools, such as: XDoclet, and Hibernate official sechmaExport own instrument. However they all have the following disadvantage: Only the input to provide some basic templates, users still need time to configure and modify; did not provide a durable type of automatic code generation, as well as the InvokeBean; do not support the graphical interface; HibernateTestCase No support for the code generation.

Hibernate tools have the following characteristics:
1. According to the database to generate UML models, automatically generated mapping file.
2. According to UML have Hibernate persistent classes.
3. Hibernate Model Checking.
4. Automatically generate test code.
5. Integration with Eclipse

Hibernate code generation tools PowerDeigner models based on extensions to Hibernate implementation of the code generation. PowerDesigner (hereinafter referred to as PD) is a leading database modeling tools (ER modeling, physical modeling), while its UML, Report, XML, team development (Knowledge Repository) are supported by pretty good, all models can be positive, the reverse conversion. Model of PD which are composed of meta-model. In short, the element model is composed of the model model. Element model based on these, PD with a set of GTL development of language, can easily expand their own templates and the code flow, but also in the existing language model adapted to the needs; PD support to expand the language to use VBScript to change the PD model, model checking. For Eclipse, PD can be integrated quickly and can be used to modify the JAVA language PD model.

Note: GTL can be said to be an object-oriented scripting language, it can be on a different element model be expanded to increase, such as: Add generated files, prototype, menu, code templates, extending the functional property and so on, in Figure 1 that will be part of the follow-up detail. Element model is because the object-oriented (such as all of the categories, such as meta-model interfaces are inherited from Classifier element model), that is, if the expansion of the Classifier in a function, then the succession of the model it have such a function and script, coverage can also rewrite this function in order to realize the concept of polymorphism.

Open the Edit window to expand the model: Choose Model? Extended Model Definition, at the pop-up window on the Toolbar select Import Extended Model Definition (penultimate button) to load the existing model, the model can also be a new expansion (select Add Row button, and then double-click in the table to edit the model can be the first pop out of the interface as shown in Figure 1)
Hibernate code generation tools for the design of the entire Raiders -大口仔- Garden blog

In order to give readers a better understanding of the concept of element model of PD, the interception of the Hibernate code generation tools used in meta-model architecture diagram, as shown in Figure 2. PD readers can find the installation directory: <pd安装路径> \ Examples \ MetaModel.oom, stored in PdOOM charts, the first name for the Class Objects.
Because of space reasons, there will be removal of a typical model code and explain how to design Hibernate code generation tools.

Hibernate code generation architecture of the overall flow (activity diagram)


Hibernate automatically generates the principles of the document are:
1) sub-categories linked to the mapping information at the root of the mapping file under the category (Root Class), that is, independent sub-class will not generate code
2) Value-Type category and the category of not lasting, will not generate code.
3) does not generate code for non-class object (such as interfaces, etc.)

One of:
Check Models in Diagram: Hibernate model checking to determine whether the DomainModel grammar Hibernate. If the error, PD will be dished out an error message (the function to provide automatic error correction)
Generate Configuration File: property selection model (Model-> Model Properties), users can set the configuration under Extended Attribute information, PD generated based on configuration information Configuration File.
Get each class in Diagram: This is the mechanism of PD, it will automatically acquire all UML model elements, and the expansion of the model according to each property (Profile \ Generated File) of the flow to generate the code file.
Generate Basic Mapping: basic mapping, it contains id, composite-id, propery and so on.
Get SubClass Style: sub-category mapping, recursive access.
Generate Join: When lasting more than one category corresponds to the mapping table, there is a need to specify the use of Join.
Generate Association Mapping: In accordance with the type of category between the Association to determine which are related to mapping, the tools support one-to-one, one-to-many, many-to-one, many-to-many (according to the Association Properties Detail under the tag to set the Multiplicity), support the collection of Array, Set, List, bag, idbag (in accordance to set ContainType).
Get Java Code Information: expansion of the model is based on the existing language model, namely the expansion of the model be able to obtain the language model and the template set, the Hibernate persistent classes in Java, we need only increase at the corresponding Java code Getter and Setter for Attribute can. % source%
Generate Invoke Bean: the CRUD functions to generate Hibernate.
Generate Test Case: Hibernate test case code, the generated random data to verify the correctness of Hibernate.
Generate Log4J: the Log4J configuration file generated
Generate ANT build.xml: When a user in the Options under the Generation Files set up Ant to True, and configure the path to Ant's Lib, the Hibernate Tools generates build.xml, then will automatically run the Ant to test Hibernate, to generate the results Log4J will be deposited into the log.

2. Technical points
O / R Mapping
PD start from 8.0, continuously strengthened on the O / R Mapping, except other than the generation of model code, PD also generate O / R Mapping definition components such as EJB CMP generated. Users can define O / R Mapping to create OOM and the relationship between PDM.
PD to support three kinds of forms of O / R Mapping:
The first one is the class diagram from the data model into the PD automatically after the connection set up (for top-down design process);
The second is to convert from the data model class diagram automatically after set up by the PD connection (for bottom-up design process);
The third category are set up after the data model diagram and manually set up by the user (for synchronous design changes or late)
Set up process:
The first is: select type of map, then the design of persistent classes (POJO), after select Tools-> Generate Physical Data Model. At the pop-up window, select the Detail tab, select the O / R Mapping option, you can. Readers can select Yes to update the existing database or new database.
The second: the first is similar to a process set up, but replaced in the database model, select Generate Object-Oriented Model
The third: the user must first set up DataSource, the directory tree on the left to find WorkSpace types of documents, right-click then select New \ Data Source, at the pop-up window, select the tag Models and want to select the database associated with the model . Open-type model in the category of property (double-click to build O / R Mapping class), select Mapping tab, click the button to add just set up the DataSource. Then click the Add data type mapping table, that is, the end set up.
The user can add more than one table, it probably corresponds to a class that many of the mapping tables, Hibernate mapping should be used to specify the Join property.
Note: When a user set up a ClassSource, PD will be based on type of property and set up form fields automatically AttributeMapping, of course, users can also AttributeMapping at its property page under the modified mapping.
As we all know, Hibernate mapping file must contain statements on the table (such as: column, property attributes and so on), in PD, through the Mapping element model can easily access information corresponding to the table. GTL in Figure 4 are used in O / R Mapping element model.
O / R Mapping architecture diagram:

Architecture Description:
1) Association:
At Asoociation element model, you can access through the Mappings to AssociationMapping (that is, users select the Mapping for the next DataSource), each has AssociationMapping will SourceClassifiers, it is within the mapped tags AsoociationSource collection as shown in Figure 5. SourceClassifiers collection of elements, that is, the Reference Data Model element model, as shown in Figure 6.
PD because of Association Mapping in the data model, add other Reference and Table, to do so in the Hibernate code generation, use was made of the type of judge, so as to avoid causing the type mismatch error.




? Implementation code (Table mapping): (Note: Appendix GTL will introduce some grammar)
. A629581d02fa0bbf63d306f364b11033

? Implementation code (referenceColumnHelper template):

2) Class and Attribute
Access to a database model similar to the methods and Association, in the O / R Mapping in, ClassMapping is equivalent to the SourceFeature Data Sheet; AttributeMapping is equivalent to the SourceFeature data listed in the table information. Figure 7 give the used in the preparation of the database element model to the architecture diagram.
? Implementation code (the table to obtain the corresponding categories of information)

Description: Mappings.First.ClassSources show that under the current Data Source Table Set
? Implementation code (to obtain the corresponding column Attribute information)
For example:% AttributeMappings.First.SourceFeature.Code%
% AttributeMappings.First.SourceFeature% corresponds to Figure 7 of the Column element model.


Extended Attribute
Tool for generating Hibernate mapping file, it is inevitable to encounter a lot of options allow users to select or enter, at this time, we can use the PD provided to expand the Extended Attribute Model property. For UML models, apart from Dependency and Generalization, the rest of the model are to expand the Extended Attribute.
Extended Attribute settings steps: extension of the model into the Edit window, select the Profile of a meta model, then select Right Extended Attribute, PD has provided a wide range of default options (add brackets), of course, increase the user's own Extended Attribute Type (in the Profile directory under the shared right-click on select Extended Attribute Type, then you can set out on the right as well as the default value of the content and so on. set up after the model the reader will be able to expand the property in reference to the type of custom), shown in Figure 8. At GTL, the property can be used% name% applied to the current value of the user's selection, the template can also be used% name% GTL reference to the template (and readers can select the template Jump F12).


Check Model
Consider the set up because of UML-based DomainModel, grammar Hibernate for example, will inevitably have some grammatical errors, such as: set up in between the two types of Association, but the corresponding table has not between the Reference, or do not have Reference Joins under Column, then we should give an error warning that prompts the user to check.
To AssociationCheck mentioned as an example, how to introduce user-defined implementation of the Check.
Hibernate extension of the model open, select the Profile \ Association, right-click to select New \ Custom Check. On the right, readers can see that there are four tab, as shown in Figure 9.


Check Script: Model for the detection of,% Check% = true, error messages will not appear.
Autofix Script: Auto-repair model for error,% Fix% = false, that does not fix the error. When the PD monitoring the error, the user can right-up in the wrong can select Automatic Correction. Auto Repair for the error, will be at the bottom right corner of the icon to add a "+", such as that the PD to achieve restoration in accordance with Autofix Script.
Global Script: used to store global function, in any element model can be called the Check Model.
Implementation code
Check Script: Detect Refence for the right set up or not.


Note: PD will test each model (including Package), so if the reader wishes to adopt procedures to control the model has been tested only in the grammar to meet the conditions above to add% Check% = True (that the right to verify and skip) can.

Persistent Class category <br /> lasting lasting for POJO class, must be increased for each type of property Getter and Setter function. Extension model is based on the language model, that is, language model, such as the contents of the template model can be found in the expansion of coverage and rewriting. Therefore, the expansion of the model in the Hibernate overloaded methods used to expand the Java language model of code generation, to retain that part of the original Java code, and the expansion of the model at his own template Getter and Setter.
Model in the Java language, code generation% source% are to rely on templates to complete the Java code, at the same time there is a template implementation% initializers% after the completion of a number of Attribute initialization work, so at the same location (Class element model) of the place % initializers% rewrite of the template code is as follows:

Attribute element model at increased getterFunction, setterFunction template code is as follows:

And in the Class element model category in a lasting increase in the Generated Files, and in the Edit Template box to enter the% source%.

Generated Files
Element model of PD in each document is provided in the function of generation. Meta Model Class at select on right-Generated Files. Window on the right, in the File Name, enter the file name after generation. Hibernate mapping file because the file name and class name the same, it enter% mappingFilename%, at the same time set up templates mappingFilename and enter% Code%. Hbm.xml.
Note: All of the element model are inherited from NamedObject (NamedObject succession in BaseObject), stored at various NamedObject element model of the standard attributes, such as: model name (Name), the code name (Code), notes, description and so on. Therefore, in order to get the names, the element model in the category, enter% Code%. Hbm.xml, then the PD would be under the code name for each type of set up the appropriate file (provided that these can be generated, has a lasting of).

Model
Model element model in the main Configuration Mapping realize the connection of the configuration information, such as, JDBC and so on. The method used is the Extended Attribute and Mapping template code. Because of space reasons, not the list, readers can refer to just said, or can be installed in the PD directory pdvbs11.chm to run for help.

Task
Task after the code generation is activated, then PD will be selected in accordance with the implementation of mission priorities. Hibernate extended model, an increase of the Lib path ANT to generate the directory name and so on options. In the Generation \ Options under the new option can be set up, similar to Extended Attribute. Call Options option, type the following code:% GenOptions. Option to obtain the name%.
For Task, want to be able to use ANT for automated testing build.xml configuration. In PD in, JAVA language model ANT has provided the build.xml and set aside the automatic generation of interfaces (customExecuteTarget, customerProperties, customTaskDefs three template) to allow for the expansion of heavy-duty models, interested readers can view the Java Language Model (Language? Edit Current Object Language, visit the Java:: Profile \ Model \ Templates \ Ant \ antTemplate).

Set up Task:
At Generation \ Tasks on the right, select New. Enter the Task Name (that is really the implementation of the display name), known as Run Unit Test. In the following table has been set up in the selection of Command, if there is no set up, you can on the new Commands.
Run Unit Test Command code:


Description:
. execute_command for the macro command for the implementation of external procedures.
. execute_command '(' <cmd> [ ',' <args> [ ',' <mode>]] ')'
The main parameters of the first order, here are CMD
The second parameter of the main parameters of the command, this is / K ANT.BAT JUNIT
The second parameter type for the pipeline, PD provides two ways cmd_ShellExecute and cmd_PipeOutput. The former process of an independent way, which will be blocked until the task is completed PD, and the results will be displayed in the Output window in PD, as shown in Figure 10.


3. Concluding remarks
PowerDesigner is a highly flexible software modeling tools, based on its element model, readers may want to use a different language (GTL, VBScript, Java, C #, etc.) to design its own code generation tools, and even the language model , Report and so on. This is only a start and played a role, I believe, by virtue of experience in the field, the reader must also be more suitable to design their own code generation tools, to streamline processes, reduce costs and speed up the development.

Appendix:
GTL listed some grammar, readers can also see under the PD installation directory pdvbs11.chm documents or the official website of Sybase.
1. Resource file reader can refer to the existing models PD11:
You can install the directory \ Resource Files \ Extended Model Definitions to find the expansion of the existing model; of installation directory in the \ VB Scripts found VBScript code; Ole Automation directory to find how to use JAVA, C # and other languages to do element model to obtain their own code generation tools; Library directory to find language model. Printable Docs in the directory to find PDF documents (Advanced User Documentation.PDF recommended reference document).
2. Grammar
a. set variable

% Variable names must be used to package, that is,% variable name%
Global Variables

Variable region that easy, when the region after the set up (such as, recycling, and the other template, etc.), in the region outside the area call the variable, you need to use Outer. Variable name to specify.
b. circle

c. determine the conditions

d. Set

e. special symbols

?


Source citation?? Hibernate code generation tools for the design of the entire Raiders -大口仔- Garden blog
Digg Technorati StumbleUpon Mixx del.icio.us Reddit BlinkList Furl YahooMyWeb feedburner

Tags: mapping framework (RSS), durable type (RSS), persistent classes (RSS), object relational mapping (RSS), code generation tools (RSS), automatic code generation (RSS), language java (RSS), java programmers (RSS), source object (RSS), xdoclet (RSS), database modeling tools (RSS), conversion model (RSS), sensitive keyboard (RSS), knowledge repository (RSS), element model (RSS), meta model (RSS), uml models (RSS), object programming (RSS), graphical interface (RSS), development knowledge (RSS)

Permalink: http://www.codeweblog.com/hibernate-code-generation-tools-for-the-design-of-the-entire-raiders-%e5%a4%a7%e5%8f%a3%e4%bb%94-garden-blog/

Leave a reply