Related Topics: Java Developer Magazine

Java Developer : Article

Enterprise Application Development Using Java Blend

Enterprise Application Development Using Java Blend

Object-oriented software development using Sun Microsystems' Java language has become an industry standard for building scalable enterprise client/server applications. Additionally, most online transaction processing (OLTP) applications developed today by enterprises use relational databases as a standard DataStore, forcing developers to be proficient in the details and intricacies of both relational database design and object-oriented programming. They also must contend with mapping objects to relational schema, object management and caching, object navigation, transaction and concurrency controls. However, until recently developing Java applications with relational databases has been a time consuming process for developers.

To simplify development of application programs that use the Java programming language objects and relational databases, Sun Microsystems is introducing a new software product called Java Blend. The primary goal of Java Blend software is to provide programmers with a tool to easily develop business applications entirely in Java. With Java Blend, developers aren't required to learn the details of relational databases or Structured Query Language (SQL).

Java Blend consists of a development tool and a run-time environment. The development tool provides bidirectional mapping that can be used to map existing relational schema into a set of Java classes or map an application model written in Java to a new database schema to store instances of Java classes. The run-time environment provides transparent and automatic conversion of database records to Java programming language objects and Java programming language objects to database records, object navigation, transactions, concurrency and object caching for high performance.

Java Blend understands a database schema and can automatically generate the default mappings based on the relationship information between the tables, whether it is one-one, one-many, many-many, foreign key or inheritance. Additionally, it features the advanced capability to modify mapping of views on multiple tables, mapping a Java class to multiple tables or multiple Java classes to one table. Features such as using different names and types in Java and the database for the same fields are also supported, of course.

Applications written in Java Blend are highly portable, not only because the code generated is 100% pure Java, but also because it is based on the Object Database Management Group (ODMG) standard for object/relational mappings and object databases. Java Blend uses JDBC to communicate to relational databases so that any database with JDBC support automatically becomes usable by Java Blend; it supports several major databases in its first release, and will support any JDBC or ODBC based database in later releases.

Java Blend supports optimistic concurrency as well as the traditional pessimistic locking. Use of the optimistic concurrency control mechanism allows Java Blend to detect the values that have changed in the program's memory and database since the last transaction began. This results in higher performance and scalability at run time.

Application development using Java Blend can be broken down into four main steps.

1. Object/Relational Mapping
Using the Java Blend tool, map your database schema to Java classes or vice versa. For example, if you had two tables - Customer and Orders - in your database that were defined by the data definition statements in Listing 1, Java Blend would generate the class definitions for Customer and Orders in Listing 2.

Java Blend automatically generates member variables for each column and accessors/mutators for each member variable. Java Blend also understands relations between tables and maps them as one-one, one-many or many-many relationships between Java classes depending on the relationship between the tables. It then generates appropriate accessors/mutators and other helper methods. For example, Java Blend figured out the one-many relationship between Customer and Orders and generated an accessor method getOrdersForCustomer that returns a collection of Orders objects and other helper methods, like addOrdersForCustomer and removeOrdersForCustomer for adding and removing Orders objects for a Customer.

Before products like Java Blend, these mappings had to be done manually by someone who understood both the data model and the object model. As this example shows, such a mapping can now be created automatically, including more complex one-many and many-many relationships.

2. Add Business Logic
Almost every business application will have some business logic. With Java Blend, the business logic can be added into the generated Java classes in the form of new methods or embedded in the accessors and mutators.

For example, the code snippet in Listing 3 shows how the method addOrdersForCustomer in class Customer can be modified to check the customer's credit history before placing the order.

3. Write Queries
Queries are often required in applications to extract data. Java Blend supports industry standard Object Query language (OQL) that has been defined by ODMG. OQL has a rich syntax that allows the writing of simple to complex queries to extract data in the form of Java objects from a relational database. For example, the following query will extract all the Orders objects with a unit price greater than $100:

Select O from Orders O Where O.unitprice > 100;

This query will be compiled using the Java Blend tool and then later instantiated in your program to get a collection of "Orders."

With OQL, queries are written using the object model, not the relational model. This makes it much more intuitive and easy for object-oriented programmers to use.

4. Write the Application
So far we have seen how the mapping works, where to add business logic and how to write an OQL query. Still missing, however, are ways to open and close the database, start and commit transactions and, a method of concurrency control.

Java Blend provides a set of Application Programming Interfaces (API) for database and OQL queries and transactions. The code snippet in Listing 4 demonstrates how some of these APIs can be used in a typical database application.

Notice that the open method takes a database URL, a user name and a password as arguments.

Starting the Transaction is as simple as creating an instance of the Transaction object and invoking the begin method. The begin method takes an argument that specifies the concurrency mode. In this case the transaction mode is optimistic. The value Transaction.PESSIMISTIC will start the transaction in the traditional locking mode.

The new and set methods on objects between the begin and the commit statements will result in changes in the relational database. For example, in this case a Customer object with customerid 1000 will be created as a row in the Customer table in the relational database.

With Java Blend software, even transactions are object-oriented, not relational oriented. This makes application development much easier for developers who are familiar with the object model but not as familiar with relational databases.

Java Blend software allows programmers to easily develop business applications entirely in Java without having to learn the details of relational databases or SQL. With Java Blend, writing Java applications that access relational databases has just become much easier.

If you would like more information on the Java platform or Java Blend software, visit, or call 1-888-THE-JAVA.

More Stories By Syed Q. Abbas

Syed Q. Abbas is a senior Java developer at Sun Microsystems in Menlo Park, CA. Syed received his master's degree in computer science from AMU Aligarh, India, and has worked in the software industry for eight years.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.