Search code examples
javaormjdodatabase

How does ORM work under the covers? Also what is the best way to have persistent objects in Java?


How does ORM work? Are objects serialized into BLOBs?

In Java, is JDO still the way to go for this? What else is available? Seems like there was a lot of talk of EJB, direct object serialization, and JDO.


Solution

  • To answer your first question, here is an extract from Hibernate in Action, that says that there are various ways to implement ORM:

    Pure relational

    The whole application, including the user interface, is designed around the relational model and SQL-based relational operations. This approach, despite its deficiencies for large systems, can be an excellent solution for simple applications where a low level of code reuse is tolerable. Direct SQL can be fine-tuned in every aspect, but the drawbacks, such as lack of portability and maintainability, are significant, especially in the long run. Applications in this category often make heavy use of stored procedures, shifting some of the work out of the business layer and into the database.

    Light object mapping

    Entities are represented as classes that are mapped manually to the relational tables. Hand-coded SQL/JDBC is hidden from the business logic using well-known design patterns. This approach is extremely widespread and is successful for applications with a small number of entities, or applications with generic, metadata-driven data models. Stored procedures might have a place in this kind of application.

    Medium object mapping

    The application is designed around an object model. SQL is generated at build time using a code generation tool, or at runtime by framework code. Associations between objects are supported by the persistence mechanism, and queries may be specified using an object-oriented expression language. Objects are cached by the persistence layer. A great many ORM products and homegrown persistence layers support at least this level of functionality. It’s well suited to medium-sized applications with some complex transactions, particularly when portability between different database products is important. These applications usually don’t use stored procedures.

    Full object mapping

    Full object mapping supports sophisticated object modeling: composition, inheritance, polymorphism, and “persistence by reachability.” The persistence layer implements transparent persistence; persistent classes do not inherit any special base class or have to implement a special interface. Efficient fetching strategies (lazy and eager fetching) and caching strategies are implemented transparently to the application. This level of functionality can hardly be achieved by a homegrown persistence layer—it’s equivalent to months or years of development time. A number of commercial and open source Java ORM tools have achieved this level of quality. This level meets the definition of ORM we’re using in this book. Let’s look at the problems we expect to be solved by a tool that achieves full object mapping.