# Hibernate.initialize() does not initialize lazy @Basic fields

**URL:** <https://discourse.hibernate.org/t/hibernate-initialize-does-not-initialize-lazy-basic-fields/8625>\
**Category:** Hibernate ORM\
**Created:** [November 29, 2023, 3:02pm UTC](https://discourse.hibernate.org/t/hibernate-initialize-does-not-initialize-lazy-basic-fields/8625 "2023-11-29T15:02:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ilia\_Basin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ilia_basin/32/2425_2.png) [@Ilia\_Basin](https://discourse.hibernate.org/u/Ilia_Basin)\
**Post date:** [November 29, 2023, 3:02pm UTC](https://discourse.hibernate.org/t/hibernate-initialize-does-not-initialize-lazy-basic-fields/8625/1 "2023-11-29T15:02:48Z")

</div>

Hi. I have an entity with a lazy @Basic field and it works. Now I want to find an entity in DB by id and force eager load of all basic fields (but not associations) with just one SQL statement. I thought that a combination of `EntityManager.getReference() + Hibernate.initialize()` would do that, but in fact, the second method only retrieves the id, but not the other columns. (The first method works as expected and does not access the DB).

```auto
            Language lang;
            // nothing
            lang = em.getReference(Language.class, 1L);
            // select l1_0."id" from "TestCache2$Language" l1_0 where l1_0."id"=?
            Hibernate.initialize(lang);
            // select l1_0."code",l1_0."name" from "TestCache2$Language" l1_0 where l1_0."id"=?
            lang.getName();
            // nothing
            lang.getCode();

    @Entity
    @Immutable
    public static class Language {

        public Language() {}

        @Id
        private long id;

        @Basic(fetch = FetchType.LAZY)
        private String code;

        @Basic(fetch = FetchType.LAZY)
        private String name;

        public long getId() {
            return id;
        }

        public void setId(final long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(final String code) {
            this.code = code;
        }
    }

```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 30, 2023, 10:59am UTC](https://discourse.hibernate.org/t/hibernate-initialize-does-not-initialize-lazy-basic-fields/8625/2 "2023-11-30T10:59:12Z")

</div>

I think if you enable enhanced proxies, then you can access the interceptor and initialize all lazy attributes of the same fetch group at once.

Something like this:

```java
lang = em.getReference(Language.class, 1L);
EnhancementAsProxyLazinessInterceptor interceptor = (EnhancementAsProxyLazinessInterceptor) ((PersistentAttributeInterceptable) lang).$$_hibernate_getInterceptor();
interceptor.forceInitialize( lang, "name" )

```

I would recommend you to rather use DTOs for this e.g. with this HQL

```auto
select new LanguageDto(l.id, l.name, l.code)
from Language l
where l.id = :id

```

or use something like [Blaze-Persistence Entity-Views](https://github.com/Blazebit/blaze-persistence#entity-view-usage).
