241

When someone talks about hydrating an object, what does that mean?

I see a Java project called Hydrate on the web that transforms data between different representations (RDMS to OOPS to XML). Is this the general meaning of object hydration; to transform data between representations? Could it mean reconstructing an object hierarchy from a stored representation?

Jim
  • 2,413
  • 2
  • 12
  • 4
  • 3
    Please also see this question, which has a great answer. It's not quite the same question, so I'm not voting to close. But it explains hydration well: http://stackoverflow.com/questions/4929243/clarifying-terminology-hydrating-an-entity-fetching-properties-from-the-db – Erick Robertson Dec 27 '13 at 15:06

4 Answers4

267

Hydration refers to the process of filling an object with data. An object which has not yet been hydrated has been instantiated and represents an entity that does have data, but the data has not yet been loaded into the object. This is something that is done for performance reasons.

Additionally, the term hydration is used when discussing plans for loading data from databases or other data sources. Here are some examples:

You could say that an object is partially hydrated when you have only loaded some of the fields into it, but not all of them. This can be done because those other fields are not necessary for your current operations. So there's no reason to waste bandwidth and CPU cycles loading, transferring, and setting this data when it's not going to be used.

Additionally, there are some ORM's, such as Doctrine, which do not hydrate objects when they are instantiated, but only when the data is accessed in that object. This is one method that helps to not load data which is not going to be used.

Erick Robertson
  • 29,556
  • 11
  • 66
  • 98
  • 11
    +1; This is a more complete answer than mine, in the context of Java and Hydration. It is still completely synonymous with the general term deserialization, as my answer stated, but is most commonly used in ORMs. This is because ORM APIs often offer control over the set of data they retrieve, allowing you to reuse an existing object mapping instead of having to write new mappings for each query you write. Might want to mention that you're talking about "lazy-loading", since this Q&A is all about terminology :) – Merlyn Morgan-Graham Dec 26 '13 at 21:37
  • 2
    Is it correct to call an opposite process of getting data from object - extraction? Extract data from object. – Oleg Abrazhaev Jul 20 '16 at 09:08
155

With respect to the more generic term hydrate

Hydrating an object is taking an object that exists in memory, that doesn't yet contain any domain data ("real" data), and then populating it with domain data (such as from a database, from the network, or from a file system).

From Erick Robertson's comments on this answer:

deserialization == instantiation + hydration

If you don't need to worry about blistering performance, and you aren't debugging performance optimizations that are in the internals of a data access API, then you probably don't need to deal with hydration explicitly. You would typically use deserialization instead so you can write less code. Some data access APIs don't give you this option, and in those cases you'd also have to explicitly call the hydration step yourself.

For a bit more detail on the concept of Hydration, see Erick Robertson's answer on this same question.

With respect to the Java project called hydrate

You asked about this framework specifically, so I looked into it.

As best as I can tell, I don't think this project used the word "hydrate" in a very generic sense. I see its use in the title as an approximate synonym for "serialization". As explained above, this usage isn't entirely accurate:

See: http://en.wikipedia.org/wiki/Serialization

translating data structures or object state into a format that can be stored [...] and reconstructed later in the same or another computer environment.

I can't find the reason behind their name directly on the Hydrate FAQ, but I got clues to their intention. I think they picked the name "Hydrate" because the purpose of the library is similar to the popular sound-alike Hibernate framework, but it was designed with the exact opposite workflow in mind.

Most ORMs, Hibernate included, take an in-memory object-model oriented approach, with the database taking second consideration. The Hydrate library instead takes a database-schema oriented approach, preserving your relational data structures and letting your program work on top of them more cleanly.

Metaphorically speaking, still with respect to this library's name: Hydrate is like "making something ready to use" (like re-hydrating Dried Foods). It is a metaphorical opposite of Hibernate, which is more like "putting something away for the winter" (like Animal Hibernation).

The decision to name the library Hydrate, as far as I can tell, was not concerned with the generic computer programming term "hydrate".

When using the generic computer programming term "hydrate", performance optimizations are usually the motivation (or debugging existing optimizations). Even if the library supports granular control over when and how objects are populated with data, the timing and performance don't seem to be the primary motivation for the name or the library's functionality. The library seems more concerned with enabling end-to-end mapping and schema-preservation.

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 54,918
  • 14
  • 119
  • 174
  • 86
    -1 Hydration refers to filling an existing object with data. It is not a synonym for serialization. – Erick Robertson Dec 26 '13 at 15:17
  • 1
    @ErickRobertson: I never wrote that it was serialization. In fact, I wrote that it was the opposite. Want to reread my answer and remove your downvote? – Merlyn Morgan-Graham Dec 26 '13 at 21:10
  • 20
    It is not a synonym for deserialization either. Deserialization involves creating the object with the data it had at the time it was serialized. Hydration refers to how you fill an object with data. It's a subtle difference, but it is a difference. You cannot, for example, partially deserialize an object. (at least, not through any standard API) – Erick Robertson Dec 27 '13 at 05:24
  • @ErickRobertson: The concept of serialization and deserialization you describe is Java centric, which I'll give you a pass on because the tag on the question is "Java". However, the question was "When someone talks about hydrating an object, what does that mean?" - which is a general question, and doesn't require a Java specific answer. Google "partial deserialization" and you might see that the concept of "hydration" is just an ORM-oriented synonym (probably minted in Java land) for a general programming concept. – Merlyn Morgan-Graham Dec 27 '13 at 10:35
  • @ErickRobertson: If I'm getting catty or combative, it's mostly because I don't like unnecessary down-votes. My answer isn't wrong, even if it doesn't address the Java slant implied by the tags. Your answer is mostly fine, too, hence why I up-voted it :) – Merlyn Morgan-Graham Dec 27 '13 at 10:36
  • 6
    We're all trying to learn. If I believe it to be un-necessary, I will remove it, and I appreciate the discussion. My answer is not Java-centric, as I mostly have used the term "hydration" with regards to PHP actually. The answer to the question "What does hydrating an object mean?" is "filling an existing object with data." The object has to exist before you can hydrate it, which is not true for deserialization. `deserialization == instantiation + hydration` – Erick Robertson Dec 27 '13 at 14:45
  • 2
    Sorry, I'm having a problem tagging you in this answer, Merlyn Morgan-Graham. It's getting removed when I save it. I also take issue with inflating being equated to deserialization. I edited the linked Wikipedia article to remove the unsourced claim. Inflating to me implies some sort of decompression routine. A serialization routine is orthogonal to compression. – Erick Robertson Dec 27 '13 at 15:00
  • 2
    @ErickRobertson Side note: the `@foo` is not needed when responding to the post author. – Duncan Jones Jan 29 '14 at 16:17
  • @ErickRobertson: [DEFLATE is a decompression algorithm](http://en.wikipedia.org/wiki/DEFLATE), so you aren't incorrect in thinking this. However google shows that some people still use the term that way (either that, or a lot of people have plagiarized Wikipedia). I understand your anecdotal evidence, but please provide some statistics and show your research on the etymology before you change the world (wikipedia) to fit your views, then my corresponding answer. ;) I don't think I was spreading dis-information. – Merlyn Morgan-Graham Feb 03 '14 at 18:23
  • @MerlynMorgan-Graham I think you meant to say [DEFLATE is a compression algorithm](http://en.wikipedia.org/wiki/DEFLATE) :) – robinmitra Dec 16 '14 at 23:59
  • 1
    as it's been pointed out in the first comment... usually it simply means to populate an object with data – vladko May 18 '15 at 21:39
  • @ErickRobertson - motivated by the crowd and your better answer, I gave this a second shot. Let me know what you think :) Thanks! – Merlyn Morgan-Graham Dec 07 '15 at 00:50
  • So is this where Firehose.io derives its name from? – Jonny May 16 '16 at 12:50
  • 1
    agree with @ErickRobertson – Oleg Abrazhaev Jul 20 '16 at 09:07
  • 1
    @OlegAbrazhaev I've gotten over my frustration, and edited the answer to attempt to further to reduce confusion. Please let me know what you think, and which parts you would personally suggest to improve. – Merlyn Morgan-Graham Jul 21 '16 at 06:16
45

While it is somewhat redundant vernacular as Merlyn mentioned, in my experience it refers only to filling/populating an object, not instantiating/creating it, so it is a useful word when you need to be precise.

J. Dimeo
  • 739
  • 7
  • 10
  • 7
    I have changed my mind, and now I agree that this answer is more accurate than my old answer. I have since edited my answer (since it was accepted, and I want it to be accurate). Now hopefully it now reflects the difference between deserialization and hydration (in particular, `deserialization == instantiation + hydration`). – Merlyn Morgan-Graham Jul 21 '16 at 06:28
18

This is a pretty old question, but it seems that there is still confusion over the meaning of the following terms. Hopefully, this will disambiguate.

Hydrate

When you see descriptions that say things like, "an object that is waiting for data, is waiting to be hydrated", that's confusing and misleading. Objects don't wait for things, and hydration is just the act of filling an object with data.

Using JavaScript as the example:

const obj = {}; // empty object
const data = { foo: true, bar: true, baz: true };

// Hydrate "obj" with "data" 
Object.assign(obj, data); 
console.log(obj.foo); // true
console.log(obj.bar); // true
console.log(obj.baz); // true

Anything that adds values to obj is "hydrating" it. I'm just using Object.assign() in this example.

Since the terms "serialize" and "deserialize" were also mentioned in other answers, here are examples to help disambiguate the meaning of those concepts from hydration:

Serialize

console.log(JSON.stringify({ foo: true, bar: true, baz: true }));

Deserialize

console.log(JSON.parse('{"foo":true,"bar":true,"baz":true}'));
jonschlinkert
  • 10,046
  • 3
  • 38
  • 50