Comments
jcl wrote: Hi,thank you for this tutorial I'm interested on the first way to intregate Spring and EJB3. I have tried it in a example project buy it doesn't run. I'm searching since many time a solution,but nothing. I have posted on Spring forum,but no one seems can help me. I appreciate if you can help me.Thank you Antonio
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


Evaluating Options for Persisting Java Objects
Hibernate, DB4O, and Caché Database with Jalapeño

We live in a relational world - which is too bad since we develop with objects. Since most non-trivial applications require information to be persisted and retrieved in what is generically called a database, we need to find efficient methods for persisting our objects and retrieving them. Historically, this has been done with relational databases and lots of code that flattens the objects and maps them to the relational tables. This can be done in Java or with object-relational mapping tools like Hibernate.

While most books and articles about object-oriented software development discuss the benefits of using objects to describe the problem space, currently most development is done in a more convoluted manner, working from both the Java and database ends and using various technologies to bridge the gap between them. Hibernate has emerged as one of the most popular ways to address this development challenge.

Instead of starting with a database schema and building objects from the tables and data therein, this article will focus on starting with the Java objects and evaluate a few of the many options for persisting them. We will compare and contrast the methods, as well as discuss challenges and problems specific to object persistence management (see Table 1).

Options for Persisting Java Objects
There are many options for persisting your Java objects. Some are best used when your persistence needs are simple (such as saving program state between sessions) and some are better when your needs are complex (such as saving lots of data over long periods of time). Some applications simply need to load data for use in configuring the application, while others require sophisticated tools for searching and filtering object sets. The latter is typical for applications that use a database for persistence and will be the primary focus here. For each project you work on, you should evaluate which persistence strategy best fits your needs based on the project requirements. The following persistence mechanisms will be discussed in this article:
• Hibernate
- The most commonly used object-relational mapping tool
• DB4Objects DB4O
- A small simple embeddable object database
• InterSystems' Caché Database with Jalapeño
- An enterprise database that lets you store POJOs via its Jalapeño technology - but also provides a JDBC/SQL interface to the objects stored in the database

Please note that there are many other object-relational mapping solutions and object databases available, the purpose of this article is simply to provide some insight as to the benefits of using these technologies versus a JDBC/DAO implementation, how they compare and to how you can use them for your projects.

Definition: DataStore - A system for storing and retrieving data. Can be relational or object-oriented.

Object Persistence Mechanism Considerations
For each of the persistence strategies outlined above, I will review the following:
• Ease of Implementation
- How much preparation and/or configuration is required to persist your objects?
• Ease of Persisting Objects
- How do you persist an object or objects?
- Is it straightforward and intuitive?
- Is it better/simpler/faster than using SQL and writing DAOs?
• Ease of Retrieving Objects
- What mechanisms are available for finding and retrieving the objects in the datastore?
• Control over Object Depth
- How many objects do you want to save or retrieve at the same time?
• Control over Object Property Breadth
- How many properties do you need access to? Can you only return those properties?
• Object Tree Traversal
- Can you access all related objects and their properties simply by traversing the object tree?
• Enforcing Referential Integrity
- How do you ensure you don't delete an object that other objects depend on?
* For example, can you delete a department if employees still exist?
- Does it support cascade deletes/updates?
• Enforcing Uniqueness
- How do you ensure that specific properties are unique in the database, such as Social Security numbers?
• Support for Indices
- Can you define indices that will enhance the performance of your queries?
• Property Constraints
- Can you control/limit the values that will be entered into the datastore?
• Security and Access Control
- How do you control who has access to the data and what they can do with it?

Ease of Implementation
One of the things we're trying to get away from is the effort to write and maintain DAOs. So let's look at what's involved in setting up your datastore and prepping your POJOs to be persisted for each of the persistence mechanisms identified.

Hibernate
Hibernate has the most complex setup of the solutions discussed here, but it's not that bad and Hibernate does provide a lot of tools to make your life easier. There are multiple ways to implement Hibernate, but since we are starting the POJOs, we'll only consider the case of adding annotations to the Java class to support Hibernate persistence. If you're using Hibernate 3.2 with Java 1.5 or above, you can use annotations to map your POJO properties to your Table columns. Using annotations is much less verbose than defining your mappings in XML files and has the additional benefit of reducing the number of files you must keep track of. In addition to annotating your POJOs you need to provide the fully qualified name of the annotated class as a <mapping> element in the hibernate.cfg.xml file.

The minimal steps for preparing to persist your objects with Hibernate are:
1)  Set up a database and create a username and password for Hibernate to access it with
2)  Annotate your Java Classes
3)  Add your class mappings, database, and user/login information to the hibernate.cfg.xml file
4)  Run the Hibernate hbm2ddl to create the schema in the database

An example of mapping in the hibernate.cfg.xml file:

<mapping class="com.egrok.hibernate.Person"/>


About Richard Conway
Richard Conway is a software developer and technology consultant with more than 15 years of technology, project management, and information services experience. He has extensive experience developing Java/Struts-based web applications. He started focusing more on Swing based developments at the beginning of 2005 and has just finished a Swing-based client/server asset management project. He lives in Miami with his wife Patricia, is currently working on an EMR application, and plays sand volleyball in his spare time.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Hi,
your article is very well done. However one point:
>DB4O doesn't provide any support for constraints.
The is only partially true.
It's more then easy to implement this in just a few simple lines using db4o callbacks.

Best
Stefan Edlich
(Author of "The Defintive Guide to db4o" Apress)

Hi,
your article is very well done. However one point:
>DB4O doesn't provide any support for constraints.
The is only partially true.
It's more then easy to implement this in just a few simple lines using db4o callbacks.

Best
Stefan Edlich
(Author of "The Defintive Guide to db4o" Apress)


Your Feedback
Stefan Edlich wrote: Hi, your article is very well done. However one point: >DB4O doesn't provide any support for constraints. The is only partially true. It's more then easy to implement this in just a few simple lines using db4o callbacks. Best Stefan Edlich (Author of "The Defintive Guide to db4o" Apress)
Stefan Edlich wrote: Hi, your article is very well done. However one point: >DB4O doesn't provide any support for constraints. The is only partially true. It's more then easy to implement this in just a few simple lines using db4o callbacks. Best Stefan Edlich (Author of "The Defintive Guide to db4o" Apress)
Enterprise Open Source Magazine Latest Stories . . .
About 10 years ago, a quiet bunch of IT revolutionaries, tired of expensive, complicated operating systems and the resources needed to support them, created a new breed of network management software and network monitoring systems. Pioneers like Solarwinds, Groundwork, Zoho (Adventnet)...
These days the popularity of Ext JS (a JavaScript library) is gaining momentum. One of the most popular widgets within Ext JS is the DataGrid. The reason – displaying data from a database is one of the most common tasks of a web application. “Out of the box” the DataGrid has functional...
PrismTech is joining forces with Nextel Engineering Systems to deliver much-needed, highly-reliable and Real-Time data management solutions. As part of its software and services offerings, Nextel Engineering will now deliver and support PrismTech’s best-in-class suite of middleware pro...
WS-BPEL 2.0 is the dominant specification to standardize orchestration logic and process automation between Web services. The BPEL model is used to assemble a set of discrete, essentially disparate, services into an end-to-end process flow to transform the existing stateless and uncorr...
Cloud computing is a game changer. The cloud is disrupting traditional software and hardware business models by disrupting how IT service gets delivered. Entrepreneurial opportunities abound as this classic disruptive technology begins to proliferate, so it is no surprise that SYS-CON'...
The newest release of Open-Xchange builds on a consistent theme as its delivers more integration with other webmail programs like Gmail, as well as the ability to incorporate contact information – the latest includes the Yahoo address book. Open-Xchange today announced enhancements tha...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON Featured Whitepapers
ADS BY GOOGLE