Defining the join relationships between tables
After tables have been added to a catalog, you can now use them to build reports. However, in your daily work, you may often be dealing with a set of tables in your queries. You will have to define relationships and create joins among different tables every time you create a new report. For XML data source, if the parent and children nodes are transformed to different tables, joins will be embodied by the parent-child relationship, which is maintained by primary key and foreign key in tables.
JReport Designer provides you with a pre-join feature. The pre-join information is one kind of resource information stored in the catalog. It is saved in a standalone file with the extension .pre, and shares the same prefix as the catalog file. It is not used at report runtime. In JReport Designer, when you open a catalog, the pre-join file is opened at the same time. When you save the catalog, the pre-join file is also saved.
Creating joins between tables
The Pre-join Editor is a convenient tool for you to predefine the relationships among tables all at once.
To predefine joins among tables in a catalog, follow the steps below:
- In JReport Designer, open the catalog on which you want to define join relationships.
- In the Catalog Browser, select a data source to activate the Pre-join button on the toolbar if it is disabled.
- Click the button, then in the Select Data Source dialog, select the date source on which the joins will be created and click OK. The Pre-join Editor window appears.
- Click the Arrange button to organize the tables that have been added to the catalog.
- Now you can make joins among tables by using mouse drag and drop. Point to one column in the source table, press and hold the mouse button, then, move the pointer to the other column in the target table and release the mouse button. A green line with a join button will be shown, linking the two columns. This represents that a join has been created.
- Repeat the above step to set up more joins.
Modifying a join
Once a join has been established, you can modify it at any time if required. To do this:
- Double-click the join button to display the Join Options dialog. See the dialog.
- If you want an outer join made, check the Outer Join option, then specify the format of the join as required: SQL92 or Use'+'.
Note: You cannot make outer joins for XML data sources.
- Edit the join conditions in the Condition panel according to your requirements.
- When done, click OK to accept the changes.
See also Join Options dialog for details about options in the dialog.
Outer Join
There are two kinds of join formats: SQL92 and Use'+'. With conventional joins, records that do not satisfy the join condition are eliminated from the result. An outer join preserves these records in the result and replaces the missing values with nulls. SQL syntax uses the left outer join if the records in the left side table are preserved and right outer join if the records on the right side table are preserved. The left side is determined by where the arrow begins and the right side is determined by the side the arrow points to. It is independent of the location of the table in the query editor. When you drag to make the join, you always drag logically from left to right even if your view in the query editor is right to left.
For example, consider the following two tables where the join arrow points from the Customer.C# to the Order.C#:
Table 1
Order |
O# |
C# |
|
101 |
001 |
|
102 |
002 |
|
103 |
004 |
Table 2
Customer |
C# |
Name |
|
001 |
GE |
|
002 |
IBM |
|
003 |
DELL |
The inner join of Customer.C# = Order.C#
will produce the following result:
JoinResult |
O# |
C# |
Name |
|
101 |
001 |
GE |
|
102 |
002 |
IBM |
The Customer LEFT JOIN Order ON (Customer.C# = Order.C#)
will produce the following result:
JoinResult |
O# |
C# |
Name |
|
101 |
001 |
GE |
|
102 |
002 |
IBM |
|
<null> |
003 |
DELL |
The Customer RIGHT JOIN Order ON (Customer.C# = Order.C#)
will produce the following result:
JoinResult |
O# |
C# |
Name |
|
101 |
001 |
GE |
|
102 |
002 |
IBM |
|
103 |
<null> |
<null> |
The Customer FULL OUTER JOIN Order ON (customer.C# = order.C#)
will produce the following result:
JoinResult |
O# |
Order.C# |
Customer.C# |
Name |
|
101 |
001 |
001 |
GE |
|
102 |
002 |
002 |
IBM |
|
<null> |
<null> |
003 |
DELL |
|
103 |
004 |
<null> |
<null> |
Defining join paths
After you have made joins among the tables in the Pre-join Editor window, you can then use them to define paths, which can be used for creating/editing queries and setting up joins in business cubes later.
To define a join path:
- Click the Paths button on the toolbar of the Pre-join Editor window.
- In the Save Pre-join Path dialog, click the New button and type a name for the new path in the Input Path Name dialog (by default the name will be Path1, Path2, etc), and then click OK. See the dialog.
- In the Select Pre-join dialog, you will see that all the joins you have made are displayed in the All Joins box. Choose the joins you want and click to add them to the left box. See the dialog.
- Click OK to dismiss the dialog and return to the Save Pre-join Path dialog.
- Click the New button and follow the steps above to form a new path. You can select Hide Joins Added in Other Paths to hide the joins that have been used by the existing paths in the All Joins box.
- After you have finished specifying paths, click OK in the Save Pre-join Path dialog to confirm the settings.
- Click the OK button in the Pre-join Editor.
Notes:
- The feature of multiple joins (more than one join existing between two tables) is supported.
- In JReport Designer, the joins in one path should never form a loop (any table in this path will have direct or indirect joins with all the other tables). If you specify a path which forms a loop, JReport Designer will prompt you to re-select the joins.
- You cannot define a path completely having all the same joins as with existing paths. If you have specified a path that is the same as an existing path, JReport Designer will prompt you to re-select the joins.
- Not all forms of joins are supported by all data bases systems; for example, MySQL does not support Full Outer Join so be sure to check your DBMS manuals.
Editing, deleting and renaming join paths
After you have specified the paths, you can edit, delete and rename them as required. To do this, click the Paths button on the toolbar of the Pre-join Editor window to bring out the Save Pre-join Path dialog. See the dialog.
- To edit or delete a path:
- Highlight the path you want to edit or delete in the Pre-join Paths box.
- Click the Edit or Delete button.
- To rename a path:
- Highlight the path you want to rename in the Pre-join Paths box.
- In the Rename box, type the new name, and then press Enter to confirm.
Saving the relationships together with a catalog
You can save the relationships by clicking OK on the Pre-join Editor toolbar. Note that the pre-join information won't really be saved to disk until you save the catalog. After you have saved the catalog, you can then use the just created pre-join to develop reports.
Notes:
- If you have made joins and defined at least one path, you can click OK to close the Pre-join Editor window.
- If joins have been made but no path defined,
- If there are no join loops among the joins: after you have clicked OK, a default path named Path1 which includes all the joins you have made will be defined by JReport Designer. A message box will then be displayed to ask whether you accept the default path (click Yes), or you can modify the default path (click No).
- If there are join loops among the joins, after you have clicked OK, the Save Pre-join Path dialog will appear for you to define at least one path.