advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Reviews : Object-relational Mapping with Cayenne ORM :

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 as—ant 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 application—it 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

How to Add Java Applets to Your Site

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.

 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 43
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Google Hopes Chrome Will Help, Not Hurt Firefox
Remember Figlets? They're Back With Zend
Microsoft Readies an App Store Competitor?
Google: Chrome Browser Will Make Money
Sam Ramji: Microsoft's Man in Open Source
Google to Shake Up Browsers With Own Launch
Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity

Code Around C#'s Using Statement to Release Unmanaged Resources
Writing Functional Code with RDFa
BitLocker Brings Encryption to Windows Server 2008
Network Know-How: Exploring Network Algorithms
Create a Durable and Reliable WCF Service with MSMQ 4.0
The Baker's Dozen: 13 Tips for SQL Server 2008 and SSRS 2008
Book Excerpt: Microsoft Expression Blend Unleashed
Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES