title: Spring Boot Data JPA doing lazy loading - not on a relation but on the loaded entity?
body: <p>I have just come upon something that I can't describe in any other way than bizarre.</p>
  <p>I have a service that is supposed to do this:</p>
  <ol>
  <li>it gets passed an external identifier of a customer</li>
  <li>it looks up the customer's internal ID</li>
  <li>then loads and returns the customer</li>
  </ol>
  <p>I'm using optionals as there is a potential chance that external identifiers can't be resolved.</p>
  <pre><code>@Transactional(readOnly = true)
  public Optional&lt;Customer&gt; getCustomerByExternalReference(String externalId, ReferenceContext referenceContext) {
      return externalIdMappingService.resolve(externalId, referenceContext, InternalEntityType.CUSTOMER)
          .map(x-&gt;new CustomerId(x.getTarget()))
          .map(customerRepository::getById);
  }
  </code></pre>
  <p>what's noteworthy is here is that: <code>externalIdMappingRepository.resolve</code> returns an <code>Optional&lt;ExternalIdReference&gt;</code> object. If that is present, I attempt to map it to a customer that I then look up from the database. <code>customerRepository</code> is a regular spring data JPA repository (source code below)</p>
  <p>However, when trying to access properties from Customer outside the service, I get an exception like this:</p>
  <pre><code>org.hibernate.LazyInitializationException: could not initialize proxy [Customer#Customer$CustomerId@3e] - no Session
      at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:176)
      at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:322)
      at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
      at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)
      at Customer$HibernateProxy$R0X59vMR.getIdName(Unknown Source)
      at CustomerApiModel.&lt;init&gt;(CustomerApiModel.java:27)
  </code></pre>
  <p>I understand that this means, that Hibernate decided to lazy load that entity. Once outside the transactional boundaries of the service, it's not able to load the data for that object anymore.</p>
  <p>My Question is: <em>Why</em> does Hibernate/Spring Data try a lazy fetching strategy when I essentially just load a specific object by ID from a Spring Data Repository and how I can disable this behaviour the right way.</p>
  <p>I'm aware that there is a couple of workarounds to fix the problem (such as allowing hibernate to open sessions at will, or to access properties of that object inside the service). <em>I'm not after such fixes.</em> I want to understand the issue and want to ensure that lazy fetching only happens when it's supposed to happen</p>
  <p>Here's the code for customer (just the part that I think is helpful)</p>
  <pre><code>@Entity
  @Table(name=&quot;customer&quot;)
  @Getter
  public class Customer  {
      @EmbeddedId
      private CustomerId id;
  
      @Embeddable
      @NoArgsConstructor
      @AllArgsConstructor
      @EqualsAndHashCode
      public static class CustomerId implements Serializable {
  
          private long id;
  
          public long asLong() {
              return id;
          }
  
      }
  }
  </code></pre>
  <p>and here's the source code of the repository:</p>
  <pre><code>public interface CustomerRepository extends Repository&lt;Customer, CustomerId&gt; {
      List&lt;Customer&gt; findAll();       
      Customer getById(CustomerId id);
      Optional&lt;Customer&gt; findOneById(CustomerId id);
      Optional&lt;Customer&gt; findOneByIdName(String idName);
  }
  </code></pre>