Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarifying terminology - What does "hydrating" a JPA or Hibernate entity mean when fetching the entity from the DB

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?

like image 401
Marty Pitt Avatar asked Feb 08 '11 03:02

Marty Pitt


People also ask

What is data hydration?

Data hydration, or data lake hydration, is the import of data into an object. When an object is waiting for data to fill it, this object is waiting to be hydrated. The source of that hydration can be a data lake or other data source.

What is a hibernate entity?

Hibernate will scan that package for any Java objects annotated with the @Entity annotation. If it finds any, then it will begin the process of looking through that particular Java object to recreate it as a table in your database! Before Hibernate will start it's scan, not only do we need to invoke the sessionFactory.

What is hydrate in web development?

In web development, hydration or rehydration is a technique in which client-side JavaScript converts a static HTML web page, delivered either through static hosting or server-side rendering, into a dynamic web page by attaching event handlers to the HTML elements.

What is entity and create entity in hibernate?

In order to define an entity, you must create a class that is annotated with the @Entity annotation. The @Entity annotation is a marker annotation, which is used to discover persistent entities. For example, if you wanted to create a book entity, you would annotate it as follows: @Entity public class Book { ... }


2 Answers

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.

like image 157
ocodo Avatar answered Oct 12 '22 06:10

ocodo


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.

like image 18
Vlad Mihalcea Avatar answered Oct 12 '22 06:10

Vlad Mihalcea