| By Richard Conway | Article Rating: |
|
| June 2, 2007 05:15 PM EDT | Reads: |
22,791 |
Note: For any non-trivial object schema you'll have to add annotations to support database specific functionality - such as relationships and indices. Annotating your Java Classes for Jalapeño works like the way it works for Hibernate. InterSystems is releasing NetBeans, Eclipse, and IntelliJ plug-ins for Caché 2007 that makes the process of adding annotations a point-and-click operation. Thus you can quickly define or modify the Object Storage in the Caché database.
An example of how a relationship is defined:
// In the Department class
@Relationship(type=RelationshipType.ONE_TO_MANY,inverseClass="Person")
public ArrayList<Person> getEmployees(); // Method to return all employees
// In the Person class
@Relationship(type=RelationshipType.MANY_TO_ONE,inverseClass="Department")
public Department getDepartment(); // Method to return the department
Ease of Persisting Objects
Once you have your
databases created and configured, persisting your objects with any of
these methods is very simple. Any of these is a distinct improvement
over using JDBC/SQL and DAOs.
Hibernate
Once your Hibernate mappings are configured, persisting an object is very straightforward:
try{
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction(); //optional
Person person = new Person("James","Hogan");
Long personID = (Long) session.save(person);
tx.commit();
session.close(); //optional
}catch(Exception e) {
}
DB4O
Once again, DB4O makes it extremely easy to persist your objects. Simply instantiate an object and then call db.set() on it.
ObjectContainer db=Db4o.openFile("C:\db4o\test.yapp");
try {
Person person = new Person("James","Hogan");
db.set(person); // It's now saved!
// commit is called implicitly when you close the container,
// so you don't really need to call it here.
db.commit(); //optional
}finally {
db.close();
}
Caché
Caché also makes it extremely simple to save your objects.
try {
/* Connect to this machine, in the SAMPLES namespace */
ObjectManager objectManager = connect (connectiontype, host, username,
password);
Person person = new Person("James","Hogan");
Object id = objectManager.save (person, false);
objectManager.close ();
} catch (Exception ex) {
}
Ease of Retrieving Objects
Storing data is only
half the equation. It must be easy to access your objects as well. Once
again all three solutions simplify the process of getting an object.
Hibernate
You can use HQL or SQL to retrieve objects using Hibernate. For example:
package hello;
import java.util.*;
import org.hibernate.*;
import persistence.*;
public class HibernateExample {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = session.beginTransaction();
List people = newSession.createQuery("from Person m order by?m.name asc").list();
System.out.println( people.size() + " people found:" );
for ( Iterator iter = people.iterator();
iter.hasNext(); ) {
Person person = (Person) iter.next();
System.out.println( person.getName() );
}
newTransaction.commit();
newSession.close();
// Shutting down the application
HibernateUtil.shutdown();
}
}
Published June 2, 2007 Reads 22,791
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
Stefan Edlich 05/24/07 05:38:44 PM EDT | |||
Hi, Best |
||||
![]() |
Stefan Edlich 05/24/07 05:38:33 PM EDT | |||
Hi, Best |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?









































