Our previous example on persistence had a simple case of a single class User, which had no dependencies on any other class.
But in a real situation, classes can have a chain of dependencies.
For example, our User class might have a property cart of type Cart. The Cart class itself might have a List of items, each of type Item. The Item class may further have a property vendor of type Vendor.
If we had to write a DAO for User, we will have to:
1. Consider all its dependencies
2. Create tables and schemas for all dependencies
3. Come up with foreign keys which correctly link the tables.
4. Come up with SQL queries which efficiently persist/update/query the tables
Additionally, we need to try and keep things database vendor independent - so that our DAOs can be ported from say MySQL to Oracle if need be.
None of this is easy.
Trying to do this yourself results in infrastructure code which is extra effort to maintain, and prone to critical bugs which can corrupt data.
Hibernate is an ORM (Object-Relational-Mapping) solution which solves most of these, and other issues.
It is Object-Oriented. This means it expects that you are trying to persist Objects to the database, and fully supports relations between objects, including collections, inheritance and polymorphism.
Hibernate defines strict rules for how Objects should be mapped to tables. Once you create tables following these rules, Hibernate can automatically generate efficient and vendor sensitive queries for persisting to and querying the database.
In one line:
Hibernate makes it easy to write DAOs, especially for classes with dependencies.
No comments:
Post a Comment