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

后端 未结 6 1091
太阳男子
太阳男子 2020-12-04 20:54

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

6条回答
  •  温柔的废话
    2020-12-04 21:12

    Since this is a very common question, this answer is based on an article I wrote on my blog.

    Entity loaded state

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

    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.

提交回复
热议问题