Providing a Complete Data Persistence Solution - Mapping Tool Types
(Page 3 of 4 )
Mapping tools fall into two distinct categories: (1) developmental tools and (2) plug-and-play tools. JNBridge LLC provides a developmental tool called JNBridge Pro. It provides bi-directional connectivity to and from Java and .NET. Another tool, called JSQLMapper from JNetDirect, offers plug-and-play connectivity to SQL Server 2000 from Java and vice versa. JSQLMapper is the tool we will now focus on.
Currently, three approaches exist for calling Java code from .NET and vice versa: (1) socket communication, (2) XML and SOAP, and (3) bridging technologies. SOAP is an XML-oriented approach that supports schemas such as XSD and Relax NG schemas. Schemas validate XML document instances by providing document structure, enforcing business model rules, and facilitating the creation of user-defined data types when needed. Additionally, JSQLMapper achieves bi-directional mappings through support for Remote Method Invocation calls.
JNetDirect provides a well-written tutorial that leads users through the steps of mapping data to a relational database such as SQL Server 2000. Let’s walk through the tutorial. Be sure to start the JSQLMapper GUI in tutorial mode by issuing the following command:
JSQLMapper_Tutorial
JSQLMapper’s wizard allows you to create a new mapping or load a pre-existing mapping. The second wizard page connects to a database provided especially for the tutorial. The drop-down list provides a list of drivers from which you choose the driver appropriate for your object-to-relational database mapping:
• JNetDirect • DB2
• PostgreSQL • MySQL (Connector/J)
• Oracle • Custom
• Microsoft SQL Server • Tutorial
Once you have made your choice, the connection to the tutorial database is established. After providing a user ID and password (sa), a list of tables residing within the database is displayed so you can select and add tables via a user-friendly GUI. Typically, because the tables exist in hierarchical format, the first table added is referred to as the Root table. Subsequently, all tables added after the root table are considered child tables of the root, thereby setting up a parent/child relationship. This makes it easy to set up your primary key/foreign relationships.
The tutorial lists four tables for your convenience: Customer, Invoice, Item, Product.
JSQLMapper requires each database row to contain a globally unique ID (GUID). The wizard allows you to select columns that comprise the table’s primary key. The wizard screen is divided in two panes. The left pane displays all fields residing in a table, whereas the right pane represents key fields selected by the user.
The next wizard allows you to select a key generator. For example, when JSQLMapper inserts new rows into the table, it utilizes the key generator to generate a key value for the new row. JSQLMapper provides a list of key generators that represent the type of identity key associated with a specific relational database. For example, DB2’s identity key generator differs from Microsoft’s identity key generator.
Once the key is generated, you need to define how the table is related to its parent. You accomplish this by identifying the primary key/foreign key relationship. In the event JSQLMapper is unable to determine the key relationship, you can do it manually. It is also possible to define a compound key as mandated by dataset rules.
Once you finish adding tables and establishing relational key mappings, JSQLMapper allows you to test the mapping without writing any code. If everything is properly defined, a new wizard window allows you to restrict your test dataset using a standard SQL WHERE clause. Enter where id = 0 to restrict your dataset to the first table, in this case Invoice. Finally, select the "read" button to have your dataset printed in XML format. This verifies that JNetDirect provides support for the Relax NG Schema as a core component of their technology.
Next: Writing XML, Reading from Java >>
More .NET Articles
More By Dwight Peltzer