23

In his book "Patterns of Enterprise Application Architecture", Martin Fowler talks about persistence patterns which are commonly found in software development and particularly in relation to ORMs.

Is there a pattern that Hibernate adheres to most closely?

Gordon
  • 312,688
  • 75
  • 539
  • 559
yuos
  • 231
  • 1
  • 2
  • 3

4 Answers4

15

Hibernate make use of several patterns:

  • Lazy load (proxing collections)
  • Unit of Work (as part of Session object)
  • probably Identity Map or something more sophisticated
  • Mapping Metadata
  • Query Object for Criterion API
  • all object relational structual patterns
dfa
  • 114,442
  • 31
  • 189
  • 228
  • Lazy load can burn you - it requires an open session to resolve :) – extraneon Aug 20 '09 at 19:51
  • 1
    It does seem to follow all of these patterns but at the higher level I am inclined to agree more with either jpartogi or duffymo that in terms of the big picture it seems to follow either domain model or data mapper. – yuos Aug 21 '09 at 22:17
12

Hibernate does not follow ActiveRecord pattern. The pattern that Hibernate adheres most closely is the Datamapper pattern.

Joshua Partogi
  • 16,167
  • 14
  • 53
  • 75
  • duffymo's response that it follows the domain model seems valid. Is it possible that both of you are correct? – yuos Aug 21 '09 at 22:15
  • A few years late but, anyway. Hibernate provides a mechanism for Object-Relational Mapping (ORM); it's an ORM framework. If you look at the description of Data Mapper pattern, it's pretty much the same as the concept of ORM. – Psycho Punch Aug 17 '13 at 03:15
2

If you're looking for design patterns explicity, then you could consider Hibernate a fancy API for implementing the Active Record Pattern:

In software engineering, the active record pattern is a design pattern frequently found in software that stores its data in relational databases. It was named by Martin Fowler in his book Patterns of Enterprise Application Architecture. The interface to such an object would include functions such as Insert, Update, and Delete, plus properties that correspond more-or-less directly to the columns in the underlying database table.

Active record is an approach to accessing data in a database. A database table or view is wrapped into a class; thus an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save. Any object loaded gets its information from the database; when an object is updated, the corresponding row in the table is also updated. The wrapper class implements accessor methods or properties for each column in the table or view.

Juliet
  • 80,494
  • 45
  • 196
  • 228
  • 6
    data mapper does seem like a closer fit than active record, as jpartogi has pointed out. would you agree? – yuos Aug 21 '09 at 22:17
  • If Hibernate followed active record it couldn't do this: "Idiomatic persistence --- Hibernate enables you to develop persistent classes following natural Object-oriented idioms including inheritance, polymorphism, association, composition, and the Java collections framework. Hibernate requires no interfaces or base classes for persistent classes and enables any class or data structure to be persistent." – juanitogan Jul 10 '14 at 20:22
1

Hibernate is obviously domain model. The objects in ORM are the domain model, so you can't do ORM without it.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • jpartogi's response that it follows Data Mapper seems valid. Is it possible that both of you are correct? – yuos Aug 21 '09 at 22:15
  • 1
    Data Mapper would be one way to ferry values out of a ResultSet and into an object. I would say that Data Mapper concentrates on extracting a row to an object, but ORM takes the extra step of dealing with more complex issues like 1:m and m:n relationships, lazy loading, caching, etc. – duffymo Aug 22 '09 at 00:35
  • 1
    Domain Model pattern does not define anything about persistence. In certain practices like Domain Driven Design, domain models are even forced to be persistence agnostic. – Psycho Punch Aug 17 '13 at 03:17
  • "Forced"? Nothing is forced. It's always a choice. Did you notice that this answer is four years old? – duffymo Aug 17 '13 at 13:08
  • In my opinion Hibernate entities typically are POJOs with a lot of getter and setters. That is, they have Data without behaviour. The Domain patter does not even need to have a database. In a domain object you tipically encapsulate a lot of logic instead of accesing the fields directly. In an hibernate object their all have setter and getter with the minimum amount of behaviour. – borjab Sep 23 '16 at 20:42