|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Feature Evaluating Options for Persisting Java Objects
Hibernate, DB4O, and Caché Database with Jalapeño
By: Richard Conway
Jun. 2, 2007 05:15 PM
Since with Hibernate, you're typically mapping to a relational database, complex objects may need to be persisted to multiple tables, adding some complexity to the implementation. To save you the effort of annotating everything, Hibernate provides many default behaviors. For example, Hibernate by default will map your class to a Table of the same name so you don't have to use the @Table annotation unless the class name and table name differ. The same goes for class properties. They will be mapped to columns of the same name in the Table. If you want to override this behavior, use the @Basic annotation. This type of intelligent behavior minimizes the work you have to do to persist a class. For our Person class example, this means that it will be persisted to the database Table named "Person," which will have three columns (id, firstName, and lastName) and the id column will be the primary key. Hibernate provides excellent control over how the primary key is assigned, but that's beyond the scope of this article. An example of a minimally annotated POJO:
import javax.persistence.Entity; An example of how a one-to-many relationship is defined:
// In the Department class - As a final time and labor saver, you can now use Hibernate's hbm2ddl tool to generate the database schema for you. While you may be splitting objects to save them in multiple tables, which is not required for the object databases, you do get very good control over the mapping process. And as we'll see later, there are also circumstances where the relational approach has advantages over a pure object approach.
DB4O
ObjectContainer db=Db4o.openFile("C:\db4o\test.yapp"); Since you're storing actual Java objects, there' no mapping required. There's also no need to annotate relationships, but you must provide properties in the objects on both sides of a relationship if you want to be able to traverse the object tree bidirectionally.
// In the Department class
Caché
The minimal steps for preparing to persist your objects with Jalapeño/Caché are as follows: An example of a minimally annotated POJO that can be persisted using Jalapeño/Caché:
public class Person{ Like DB4O, since Caché stores objects, there's a one-to-one relationship between your POJOs and the Caché Object Classes defined in the database - so no mapping is required. Reader Feedback: Page 1 of 1
Your Feedback
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||