|
The Runtime Application
Part of the runtime portion of this example app demonstrates a 'select all' query, an insert query, and a join query. For this, you should use DWR (Direct Web Remoting)an API for developing AJAX-based applications in Java, and a Tomcat server to deploy the application. Delving into the details of the AJAX implementation is beyond the scope of this article, so we'll restrict ourselves to the Cayenne functionality. Download the application (CayenneSample.war), containing the source code of the application, the necessary .jar files (cayenne.jar and dwr.jar) and other resources including .html files and an .ant build file. The build file has been written for Tomcat and you may have to make some changes to the build.properties file before you run ANT.
Build
To compile and deploy, simply execute the 'deploy' task on the command line asant deploy. The configuration files (cayenne.xml, SampleDomainMap.map.xml, and SampleDomainNode.driver.xml) will be deployed into WEB-INF folder. The cayenne.jar file will be copied into the WEB-INF/lib folder and the compiled Java classes that were generated by CayenneModeler will be copied to the WEB-INF/classes folder.
Sales Order Application
To create the sales order application, create two Java classes:
- EmployeeDAO.java: Accesses information from the employee table.
- SalesDAO.java: Accesses information from the sales table.
Both EmployeeDAO.java and SalesDAO.java provide access to the DataContext using the method call.
DataContext dataContext = DataContext.createNewDataContext();
Select Data
Now that you've got the DataContext, you're ready to retrieve data from the database. To do this, use a SelectQuery class that comes with Cayenne. In this case, use the default properties of the class, which will retrieve all the data in the Customer table. You can set the attributes in this class to make the query more specific. Specify that you need all the data in the customer table by passing in the Customer class as an argument to the SelectQuery object.
SelectQuery selectQuery = new SelectQuery(Employee.class);
List employeesList = dataContext.performQuery(selectQuery);
Executing the query returns a list of Customer Objects.
Inserting Data
To insert a new customer record into the database, create a new Customer object and register it with the DataContext. Then call the commitChanges method and a new record is created in the database table:
Employee employee = new Employee();
dataContext.registerNewObject(employee);
employee.setName("New Employee");
employee.setSalary(2550.50);
employee.setCommission(2.34);
Employee employee = (Employee) dataContext.createAndRegisterNewObject(Employee.class);
Simple Join
The Sales table has foreign keys from all the other three tables: Employee, Product, and Customer. Now, working in a relational model, in order to get a dataset-comprising the Customer name, Product name, Quantity of product sold, Value of product sold, and the name of the sales person, you would have to go for a join of all the tables. But with Cayenne, this operation becomes extremely simple. Since you've created the relationships toCustomer, byEmployee, and ofProduct in the Sales table, you get object access to these entities. Thus, getting the customers name is a matter of accessing the getter property of the Customer object. The same holds for the other objects:
SelectQuery selectQuery = new SelectQuery(Sales.class);
List salesList = this.ctxt.performQuery(selectQuery);
Sales[] sales = new Sales[salesList.size()];
salesList.toArray(sales);
//To access the first record
Sale sale1 = sales[0];
String customerName = sale1.getToCustomer().getName();
String productName = sale1.getOfProduct().getName();
int quantity = sale1.quantity;
double amount = sale1.amount;
String salesperson = sale1.getByEmployee().getName();
Issues
Cayenne does have a few issues:
- It does not reverse-engineer all the foreign key relationships in the tables.
· The application had a problem reading the Cayenne.xml file from the WEB-INF folder, so it had to be copied to the classes folder.
- There were some insert exceptions when using the MYSQL driver (Connector J 3.1.6). The problems were fixed when the Connector J 3.1.12 MYSQL driver was used.
Cost-effective and Flexible
Many ORM and/or JDO tools on the market share much of Cayenne's functionality. However, most of them are proprietary and cost a lot. Cayenne is an open source, production-quality ORM tool that compares well with most of the ORM tools on the market in terms of features. Moreover, while most ORM tools force you to follow a custom XML schema, Cayenne uses standard XML schemas and abstracts the XML schema from you by providing a user interface-based modeler that is easy to install and intuitive to use.
Currently, you can use Cayenne modeler Eclipse, but you would have to open it as a separate applicationit would be nice to have the Cayenne Modeler as an Eclipse plug-in. It would also be nice to have a UML-style depiction of the generated classes and the API. This would be helpful for application designers to extend Cayenne functionality tailored for them. The Cayenne generated classes are less object-oriented and more relational in nature. Therefore, it would be good to have a mechanism to generate object-oriented code with inheritances and interfaces. Let's hope the next version of Cayenne takes some of these suggestions to heart.
In March 2006, Cayenne was accepted to the Apache incubator and is in the process of transition. This means it will become a regular Apache project as soon as it adheres to the Apache standards. Once the transition is done, you can expect Cayenne to be integrated with other Apache projects like Struts, iBATIS, and Geronimo.
Resources
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|