Feature
Evaluating Options for Persisting Java Objects
Hibernate, DB4O, and Caché Database with Jalapeño
Jun. 2, 2007 05:15 PM
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
About Richard ConwayRichard 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.