Welcome!

Java Authors: Maureen O'Gara, Bruce Armstrong, Liz McMillan, Walter H. Pinson, III, Yakov Werde

Related Topics: Java

Java: Article

Evaluating Options for Persisting Java Objects

Hibernate, DB4O, and Caché Database with Jalapeño

Support for Indices
Can you define indices that will enhance the performance of your queries?

Hibernate
Use the annotation @Index to cause an index to be created for the specified column or columns.

DB4O
You can define indexes in your DB4O configuration method before you open the object container. For example:

Db4o.configure().objectClass(Foo.class).objectField("bar").indexed(true);

Caché
Caché provides powerful indexing options. Besides specifying that the property must be unique, you can specify the following index types:

type = "" (default standard index), bitmap, bitslice, index, and key. For example:

    @Indices({
       @Index(name="IndexOnName", columnNames={"Name"}),
       @Index(name="IndexOnSSN", type="bitmap", columnNames={"SSN"})
    })

BitMap indices provide extremely high performance filtering for columns that have a limited number of possible values (such as categories) or which have a fixed number of characters (such as Social Security Numbers).

Enforcement of Property Constraints
Can you limit or control the values that will be saved to the database?

Hibernate
With Hibernate, you are limited to specifying that a property be NOT NULL or UNIQUE, although you may be able to specify constraints in the underlying database.

DB4O
DB4O doesn't provide any support for constraints.

Caché
Caché provides @PropertyParameter and @PropertyParameters annotations so you can control the values entered into a property. You can specify a maximum value, minimum value, and even an input pattern.

   @PropertyParameter (name = "PATTERN", value = "3N1\"-\"2N1\"-\"4N")
   public String ssn;

   @PropertyParameter (name = "MINVAL", value = "0")
   public float balance;

Security and Access Control
How do you control who has access to the data and what they can do with it?

Hibernate
This can be performed programmatically or via the underlying database.

DB4O
You can encrypt and password-protect the database file, but there are no other user access controls. Once you have access to the database, you have access to all the data in it. If you want to control access to specific data or objects in the database, this can only be done programmatically.

Caché
This can be done programmatically or via the Caché System Management Portal. You can specify which objects the user has access to as well as the level of access (ALTER, SELECT, INSERT, UPDATE, DELETE, and REFERENCES)

Portability
How portable is the solution? Is there a vendor tie-in?

Hibernate
By definition and purpose, Hibernate helps make your application database-independent - so long as you stick to standard SQL and don't use database-specific functionality. This can be useful if you want to prototype your application on a lightweight database and move it to a more robust database later at production, however I feel that it's typically better to match your development environment to the production environment as much as possible. I've also seen very few instances where an application has been migrated to another database except in extreme legacy systems.

DB4O
With DB4O, you could say there's a vendor tie-in, but you can always add Hibernate annotations to your POJOs and run a script to read in your data from DB4O and save it to your Hibernate persistence layer.

Caché/Jalapeño
Caché stores objects using sparse arrays, so it's not your typical relational database. However, you can access data as objects or via Caché's SQL projection - which makes it look and act like a relational database (to your JDBC applications at any rate). Interestingly enough, you can also use Jalapeño to export your Caché class schema to a DDL file that can be imported into a relational database. You can then use Hibernate to map your objects to the new relational schema - or continue to use the Jalapeño Object Manager to interact with the new data source. The Object Manager automatically uses object persistence methods (Open, Save, New, Delete) when accessing Caché, and relational persistence methods (Select, Update, Insert, Delete) when it's configured to connect to a relational database.

Conclusion
While you can eliminate mapping your objects to relational tables altogether, using DB4O or Caché, for example, it appears that some work must always be done if you want to take advantage of advanced database/datastore features such as enforcing referential integrity and uniqueness.

Hibernate has come a long way since it was first released. It has a bewildering number of options for configuring your object persistence mappings and behavior - as well as great tools to make it if not painless then at least not so painful.

If you want to quickly persist your objects for a small project and you can manage uniqueness and referential integrity within your application code - look no further than DB4O. It just doesn't get any easier.

The Caché/Jalapeño combination provides a compelling option for quickly persisting your Java objects with a minimum of effort while providing excellent control over database-specific functionality.

While you were busy programming your last tour de force, your peers and technology vendors have been busy building tools that enable you to do things that were previously impossible. You owe it to yourself and to your clients to pause once in a while and survey the state-of-the-art in databases and development tools to see where new entries can save you time and effort. For a comparison of features, go to the online version of this article at http://jdj.sys-con.com.

Resources
• Comparative Study of Persistence Mechanisms for the Java Platform.
http://research.sun.com/techrep/2004/abstract-136.html
• Mark Weisfeld. The Object-Oriented Thought Process. SAMS Publishing. This is an excellent analysis of what object-oriented design is all about and how it compares to procedural programming.

Java Serialization:
• Discover the secrets of the Java Serialization API.
http://java.sun.com/developer/technicalArticles/Programming/serialization/
• Bruce Eckell. Thinking in Java. www.mindview.net/Books/TIJ/

Hibernate:
• Web site: www.hibernate.org/
• Dave Minter and Jeff Linwood. Beginning Hibernate. Apress. 2006.

InterSystems' Caché Database:
• Web site: www.intersystems.com/Cache
• Jalapeño: www.intersystems.com/Jalapeno

DB4O:
• Web site: www.db4o.com/
• Simple Object Persistence with the db4o Object Database.
www.onjava.com/pub/a/onjava/2004/12/01/db4o.html

More Stories By 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.

Comments (2) View Comments

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.


Most Recent Comments
Stefan Edlich 05/24/07 05:38:44 PM EDT

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 05/24/07 05:38:33 PM EDT

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)