Auto Mappings for Forward, Reverse, and Meet-at-the-Middle Engineering Approaches
Exadel Studio allows you to automatically generate Hibernate mappings using the following approaches:
- Forward Engineering (Object-to-Schema)
- Reverse Engineering (Schema-to-Object)
- Meet-at-the-Middle
Forward Engineering (Object-to-Schema)
In this approach, we are basing the database side on the application side's object model.
- To start mapping using a forward engineering
approach, right-click the Hibernate configuration
file and select Add Persistent
Classes... from the context menu.
- In the Add Persistent Classes Wizard, click on
the Select classes...
button and select the classes you want to make
persistent.
After selecting the classes, click OK and then Next.
- In the last screen, you can click on the
Auto Mapping
Settings... button to change any Auto
Mapping settings for the given project. (These settings are explained in a separate Auto Mapping Settings Reference.)
- In this same last screen, you can choose to
generate mappings for class attributes by checking
the Generate Mapping check box. You can also
provide an optional database schema name for
tables.
- To create mappings, click the Finish button. Once generation is
finished, you can see the mappings for your classes
in the ORM Explorer view.
-
If the Generate Mapping option was selected, then Exadel Studio will mark the selected classes as persistent and will generate mappings for the selected class fields.
For collections, the type of elements will be determined via a search. Then, either a collection of components or a one-to-many association will be created depending on whether the elements are persistent or not. The search begins within the current package, then the whole project is searched if necessary.
-
If "Generate Mapping" was not selected, then no field mappings will be generated, but the selected classes will be marked as persistent and will be visible in the ORM Explorer view.
To generate mappings later, on a class-by-class basis, right-click the class name in the ORM Explorer view and select Auto Mapping from the context menu. Mappings will be generated for all relevant class fields (static and transient fields cannot be saved into a database and will be ignored).
It is also possible to generate mappings for a particular field only. Right-click the field in the ORM Explorer view and select Edit Mapping... from the context menu.
-
Reverse Engineering (Schema-to-Object)
In this approach, we are deriving the application side's object model based on an existing database.
-
To start Reverse Engineering, right-click the Hibernate configuration file and select Reverse Engineer Database Schema... . Set all values for your specific database connection.
You can test the connection by entering a user name and a password and then clicking the Test Connection button.
- In the second screen, click on the
Select tables...
button to select which tables or views to reverse
engineer.
After selecting the tables, click OK and then Next.
- In the next screen, you must provide a package
name for where Java classes as well as Hibernate configuration
files will be generated. Also on this
screen, you can invoke the Auto Mapping Settings
wizard. Finally, you can specify if mappings should
be generated by checking the Generate Mapping check box.
- To create mappings, click the Finish button. If Generate Mapping is checked, then, inside
the package selected, new Java classes will be
generated. If the package doesn't exist, it
will be created.
The reverse engineering does not create persistent classes for the following categories of tables:
- link tables in many-to-many relationships
The generated Java classes fields are determined by the type of mapping with consideration to Java reserved words. The following table summarizes the naming convention used:
Mapping Type Field Name property/identifier column name composite identifier compositeId many-to-one If the referenced class has a: - simple id, then the column name is used
- composite id, then the referenced class name is used
many-to-many referenced class name + "Many" one-to-many referenced class name
Meet-at-the-Middle
This approach can be applied if you already have your objects and schema and just want to create mappings. To start the Meet-at-the-Middle engineering approach, you first need to complete the Reverse Engineering steps described above. When entering a package name, just provide a package that already contains object model Java classes and run the auto mapping generation.
During mapping generation, a search will be done to locate an existing Java class with the same name as the database table in the package specified. If such a Java class is found, then, if possible, its existing fields will be used for mapping (no new fields will be created in such a case). Fields will be matched according to the table above that describes the naming convention.