HomeDigital EditionSys-Con RadioSearch Java Cd

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 http://java.sun.com, or call 1-888-THE-JAVA.

About the Author
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. Syed can be reached at [email protected]


Listing 1.
CREATE TABLE Customer ( 
     Name VARCHAR(32), 
    Address VARCHAR(100) 

     CustomerId INTEGER NULL REFERENCES Customer, 
     Item VARCHAR(20), 
     UnitPrice NUMERIC, 
     Quantity  INTEGER 

Listing 2.
// Customer class 
public class Customer implements PersistenceCapable { 
        private int customerid; 
        private String name; 
        private String address; 
        private DCollection ordersForCustomer; 

        // Accessor methods 
        public int getCustomerid() { 
                return customerid; 
        public void setCustomerid(int customerid) { 
                this.customerid = customerid; 

        public String getName() { 
                return name; 
        public void setName(String name) { 
                this.name = name; 

        public String getAddress() { 
                return address; 
        public void setAddress(String address) { 
                this.address = address; 

        public Iterator getOrdersForCustomer() { 
                return ordersForCustomer.iterator(); 
        public void addOrdersForCustomer(OrdersordersForCustomer) { 
        public void removeOrdersForCustomer(OrdersordersForCustomer) { 

        // Constructors 
        public Customer (Database db, int p_customerid) { 
                this.customerid = p_customerid; 

// Orders class 
public class Orders implements PersistenceCapable { 
        private int orderid; 
        private String item; 
        private long unitprice; 
        private int quantity; 
        private Customer customerForOrders; 

        // Accessor methods 
        public int getOrderid() { 
                return orderid; 
        public void setOrderid(int orderid) { 
                this.orderid = orderid; 

        public String getItem() { 
                return item; 
        public void setItem(String item) { 
                this.item = item; 

        public long getUnitprice() { 
                return unitprice; 
        public void setUnitprice(long unitprice) { 
                this.unitprice = unitprice; 

        public int getQuantity() { 
                return quantity; 
        public void setQuantity(int quantity) { 
                this.quantity = quantity; 

        public Customer getCustomerForOrders() { 
                return customerForOrders; 
        public void setCustomerForOrders(CustomercustomerForOrders) { 
                this.customerForOrders = customerForOrders; 

        // Constructors 
        public Orders (Database db, int p_orderid) { 
                this.orderid = p_orderid; 

Listing 3.
public void addOrdersForCustomer(Orders ordersForCustomer) { 
// check the credit history before adding the order 
if ( CreditHistory.getCreditHistory(this) == CreditHistory.BAD){ 
                System.out.println("Sorry, bad credit history"); 
        // customer has a good credit history 

Listing 4.
// open the database 
Database db = Database.open("jdbc:microsoft://thestork:1433/orderentry", 
"scott", "tiger"); 

// start a transaction to create a new Customer 

Transaction t = new Transaction(); 
Customer c = new Customer(db, 1000); 
c.setName("Foor Bar"); 
c.setAddress("901 San Antonio Road, Palo  
Alto CA-94303"); 
// do some more stuff like create orders for this customer etc..  
try { 
} catch (RetryException re) { 
        System.out.println( "The transaction did not go through. Try 

// close the database 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]
