April 16, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Accessing Data Through Persistence Frameworks, Page 2

  • May 18, 2004
  • By Yasser EL-Manzalawy, Yasser EL-Manzalawy
  • Send Email »
  • More Articles »

Mapping File

The core task of using any persistence framework is specifying how to map an object to a database table. Some persistence frameworks, such as JRelational, allow the developer to hard code the mapping information in the implementation of the persistent class. Other frameworks, like Castor JDO, allow the developer to specify mapping information in (a) separate file(s) called (a) mapping file(s). By following the second approach, developers can change the object-relational mapping without the need to recompile their Java code.

The Castor JDO uses an XML-based file to specify the mapping information. The following is a high-level example of a mapping file.

<mapping>
   <class>
      <map-to />
      <field>
         <sql />
      </field>
      ...
   </class>
</mapping>

Listing 2: Castor JDO Mapping File

Each persistent class is represented by a <class> element and the corresponding database table is specified by the <map-to> element. Each <field> element represents a member in the persistent class and the <sql> element represents the field in the database table to which the <field> element maps.

Mapping Difficulties

Mapping a Java class to a relational database table is not always straightforward. A Java class may exhibit the following relationships between objects:

  1. one-to-one
  2. one-to-many
  3. many-to-many
  4. class dependent on another
  5. class related to another
  6. class derived from another

This is where Castor JDO comes in handy. Castor JDO facilitates the creation of complex persistent Java objects (for example, objects with collection members or composite objects) allowing persistent objects to mirror the complex structure of the underlying relational database (as in tables with foreign keys).

Interested readers are encouraged to browse the related links at the end of this article for a group of tutorials that help you get started with Castor JDO and practice how to map a real-world database model.

Implementing Persistent Classes

Castor's Java objects look similar to JavaBeans components. Therefore, a persistent object contains a pair of (getter/setter) methods for each member variable that will be mapped to a field in the database table. More code may be added when the persistent object exhibits a to-many relationship. Listing 3 is an example of a Castor persistent object.

public class Product
{
   private int       _id;
   private String    _name;
   private float     _price;

   public int getId()
   ...

   public void setId( int anId )
   ...
  // more methods ...
}

Listing 3: Persistent object example

Object Query Language

Now, instead of directly accessing database tables, you will work with persistent objects. SQL is a data manipulation language suitable for querying relational databases. SQL is not suitable for querying persistent objects, so most persistence frameworks provide their own object query language. Like other persistence frameworks, Castor JDO uses its own object query language (OQL) for querying persistent objects. OQL is much simpler and relieves the developer for the complexity of SQL.

Conclusion

Persistence frameworks are promising tools to persist your data. They relieve you from your own buggy persistence layers and complicated JDBC and SQL code. In addition to commercially available JDO products, a number of robust and widely-used open source persistence frameworks exist. In this article, you learned the basic fundamental steps to follow when building a persistence layer using Castor JDO. Basically, these steps are similar in other persistence frameworks.

Acknowledgements

The author would like to thank Rosemarie Graham for suggesting the idea of this series and Developer.com for publishing the series.

About the Author

Yasser EL-Manzalawy has been a Java programmer and instructor since 1998. He is currently an assistant lecturer at the Systems & Computers Engineering Department, AZHAR University, Cairo. His Ph.D. research project aims to extend the Java language for agent-based systems.

Related Links

The Castor Project homepage provides the latest release of Castor JDO and there you can browse a wealth of articles and documentation materials.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel