80

In the context of ORM / Lazy loading of entities, my understanding of the term "Hydration" is as follows:

"Hydrating" describes the process of populating some or all of the previously unpopulated attributes of an entity fetched using lazy loading.

Eg: class Author is loaded from the database:

@Entity
class Author
{
     @Id
     long id;
     List<Book> books;
}

Initially, the books collection is not populated.

It is my understanding that the process of loading the books collection from the database is referred to as "Hydrating" the collection.

Is this definition correct, and is the term common place? Is there another more common term I should be using for this process?

Vlad Mihalcea
  • 103,297
  • 39
  • 432
  • 788
Marty Pitt
  • 26,266
  • 34
  • 115
  • 190
  • 3
    To the person who voted to close the question : I've added an example to help clarify what I'm asking. Please comment if it's still unclear and let me know what I could clarify. – Marty Pitt Feb 08 '11 at 03:29

6 Answers6

134

Hydrate began as a term for populating an instantiated (but empty) value-object/model from a db, (specifically in Hibernate.)

Various other ORMs and tools like BizTalk use Hydrate and other related terminology, (e.g. BizTalk uses the term Dehydrated to mean an instance is available but not yet populated.)

Personally I'm averse to redundant terminology overhauls, populated means the same thing, without re-inventing language. It adds nothing and leads to confusion (common first thought on encountering re-invented terms: is this somehow different and magical?).

The BizTalk extension of this style of language, specifically Dehydrated is redundant. I expect people haven't forgotten how to say, empty, or clear?

Hydrated and its related metaphors are essentially marketing tools, invented to differentiate Hibernate from competing products.

At this point Hibernate and other ORM products have used these terms for many years, so Hydrate (and Dehydrate) are here to stay.

ocodo
  • 27,324
  • 15
  • 97
  • 113
  • 18
    Of course it is, however, it was first, it's simple and as such it's far less convoluted. You may have also realised at this point that pretty much everything on the software layer, even a bit value being "true/false" or "1/0" is metaphorical... Should we start calling 'true' something else now? How about 'almost certainly' in respect to Heisenberg? – ocodo Apr 04 '11 at 20:46
  • 16
    I think "hydrate" is a far better metaphor than "populate". Populate brings to mind a group of colonizers moving to a foreign or virgin territory and "populating" it. There is an empty space and you fill it with something unrelated (but presumably belongs there). Where when hydrating something, say a dried fig, the essence of the substance is there but it lacks fullness. That's exactly what happens when you "hydrate" an object. Far from being "marketing fluff", hydrate is an excellent metaphor. – Kyle Mathews Jan 07 '14 at 23:16
  • 4
    @KyleMathews Well, it's three years on since I posted my answer, I didn't make explicitly clear that "Populated" was the pre-existing term for the same activity, at this point "Hydrated" is still a marginal term, and is only used in certain language/product cultures, while "Populated" remains as the generic and more broadly used term. By all means go ahead and use "Hydrate" if it pleases you, personally I find it pretentious and affected. – ocodo Jan 08 '14 at 14:51
  • 21
    If you use "hydrate" around people who don't understand that word, you're going to have to explain it. Why not just use a word that's already going to be understandable by your audience, even if it's not the most precise word possible? – Samantha Branham Feb 19 '14 at 19:40
  • 2
    While I agree that the choice of the term 'hydration' might be disputable (it does sound a bit too obscure), especially since it doesn't immediately imply loading of the data from a data store into memory, I think it is a distinct process from 'population', which normally refers to filling abstract data types such as arrays, lists, trees and tables with elements, irrespective of whether it is in memory or in a persistent storage. We populate an array with all the users, we populate a table with all the transaction records. We hydrate a new product instance with its data. – jbx Sep 09 '14 at 11:51
  • How about _inflate_ and _deflate_ in that context? _[yes, I am aware it's an old question and an old discussion]_ – ZenMaster Nov 09 '15 at 17:36
  • 1
    As it happens deflate / inflate is old compression terminology used by [lha/lzh](https://en.wikipedia.org/wiki/LHA_(file_format)) – ocodo Nov 10 '15 at 11:44
  • 8
    The fact that nobody knows what hydrate is supposed to mean without looking it up shows that it is a stupid buzz word that is unfortunately now entrenched in many ORM frameworks. – BadHorsie Sep 14 '16 at 22:30
  • I could've said that as my answer, but it's a little blunt. Can't say I disagree though. – ocodo Sep 15 '16 at 00:58
13

Entity loaded state

When you are fetching an entity, Hibernate will try to load it either from the second-level cache or the database.

Entity loaded state

If the entity is not stored in the second-level cache, then a query is executed and the JDBC ResultSet is transformed into an Object[] that contains the loading-time entity property values.

The second-level cache stores this Object[] when caching an entity. So, when loading an entity either from the DB or the second-level cache, you will get the Object[] entity property value array.

The process of transforming the Object[] loaded state into a Java entity object is called hydration, and it looks as follows:

final Object[] values = persister.hydrate(
    rs, id, object,
    rootPersister, cols, eagerPropertyFetch, session
);

The loaded state is saved in the currently running Persistence Context as an EntityEntry object, and it will be used later for the default dirty checking mechanism, which compares the current entity data against the loading-time snapshot.

The loaded state is also used as the cache entry value for the second-level entity cache.

The inverse operation of transforming the entity to an Object[] that's used when binding SQL parameter values for INSERT, UPDATE or DELETE statements is called dehydration.

Vlad Mihalcea
  • 103,297
  • 39
  • 432
  • 788
4

hydration is a loose term. In our company we use "rehydration" as he term to load all the object properties of an entire object graph. Here is a post that talks about various levels of hydration (again this is a general usage though they are using in the context of hibernate).

Aravind Yarram
  • 74,434
  • 44
  • 210
  • 298
2

I think the term 'hydrate(s)' in the context of ORM simply means the framework gives you objects. So the objects are 'hydrated' by the ORM after the data is pulled from the store. The term can be applied anytime an ORM framework gives you an object/graph that is represented in the store.

hvgotcodes
  • 109,621
  • 25
  • 195
  • 231
0

the term hydration is extensively used in the guts of the hibernate library to refer to the process of setting the fields of a recently loaded object, and is indeed related to the object graph populaton.
but it's different than the concept of lazy loading, that is, giving the user a half-filled object and letting the rest be loaded on demand.
the hydration is always performed, lazily or eagerly and it's hibernate stuff.
lazy loading is just for convenience

replace hibernate with the name of your orm of choice

jpertino
  • 2,091
  • 13
  • 9
0

Hydration is a general ORM domain term meaning a method by which the query result is returned. It's not a process, not a verb, not an action or event that occurs but a noun. Therefore hydrating can only mean using a hydration, i.e. using that specific method, nothing else and brings nothing by itself therefore should never be used. A specific hydration can instantiate an object and populate it before returning its reference but hydrating in general doesn't mean populating. Different hydrations return different structures:

  • singular scalar
  • array of scalars
  • array of arrays
  • array of objects
  • object collecting scalars
  • object collecting arrays
  • object collecting other objects
  • ...more

It's an ORM implementation detail. Some ORMs provide multiple hydrations and you can choose one by passing an argument to query builder, some don't give you that control and replace it by convention trying to be smart about it which usually leads to false assumptions.

cprn
  • 909
  • 1
  • 11
  • 20