问题
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?
回答1:
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.
回答2:
In Hibernate nomenclature, hydration is when a JDBC ResultSet is transformed to an array of raw values:
final Object[] values = persister.hydrate(
rs, id, object,
rootPersister, cols, eagerPropertyFetch, session
);
The hydrated state is saved in the currently running Persistence Context as an EntityEntry object, which encapsulated the loading-time entity snapshot. The hydrated state is then used by:
- the default dirty checking mechanism, which compares the current entity data against the loading-time snapshot
- the second-level cache, whose cache entries are built from the loading-time entity snapshot
The inverse operation is called dehydration and it copies the entity state into an SQL INSERT or UPDATE statement.
回答3:
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).
回答4:
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.
回答5:
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
回答6:
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.
来源:https://stackoverflow.com/questions/4929243/clarifying-terminology-hydrating-an-entity-fetching-properties-from-the-db