title: No Session with spring.jpa.open-in-view=false
body: <p>I have a Spring Boot 2.1 app with a simple rest controller, service tier, and data tier.  I am attempting to disable spring.jpa.open-in-view as I want to manage the loading of my collections, etc.  My problem seems to be when I disable open-in-view, I can't get anything back in my controller including simple properties including Strings.  </p>
  
  <p>Sample Project, simplified version of the code below:</p>
  
  <p><a href="https://github.com/csyperski/springbootopeninview" rel="nofollow noreferrer">https://github.com/csyperski/springbootopeninview</a></p>
  
  <p>Here is my error: </p>
  
  <pre><code>2019-02-05 09:06:19.591 -ERROR 11355 --- [nio-8081-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    :  175 : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.LazyInitializationException: could not initialize proxy [com.cwssoft.pscafepos.model.Item#268] - no Session] with root cause
  -
  org.hibernate.LazyInitializationException: could not initialize proxy [com.cwssoft.pscafepos.model.Item#268] - no Session
      at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
      at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
      at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
      at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
      at com.cwssoft.pscafepos.model.Item$HibernateProxy$h2GPuZuU.getBuilding(Unknown Source) ~[main/:na]
      at com.cwssoft.pscafepos.controllers.ItemController.lambda$getSingle$3(ItemController.java:52) ~[main/:na]
      at java.util.Optional.filter(Optional.java:178) ~[na:1.8.0_201]
      at com.cwssoft.pscafepos.controllers.ItemController.getSingle(ItemController.java:52) ~[main/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar:9.0.13]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320) ~[spring-security-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
  </code></pre>
  
  <p>Here is my Model without getters/setters:</p>
  
  <pre><code>import com.cwssoft.pscafepos.model.converters.BooleanToStringConverter;
  import javax.persistence.*;
  import java.util.Date;
  
  @Entity
  @Table(name="items", indexes = {
          @Index(name="key_items_name", columnList="item_name"),
          @Index(name="key_items_building", columnList="item_building"),
          @Index(name="key_items_visible", columnList="item_visible"),
          @Index(name="key_items_free", columnList="item_allowfree"),
          @Index(name="key_items_reduced", columnList="item_allowreduced"),
          @Index(name="key_items_typea", columnList="item_istypea"),
  })
  public class Item implements DomainObject {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "item_id", updatable = false, nullable = false)
      private Long id;
  
      @Column(name="item_name", length = 100, nullable = false, unique = false)
      private String name;
  
      @Column(name="item_description", columnDefinition = "TEXT")
      private String description;
  
      @Column(name="item_price")
      private double price;
  
      @Column(name="item_building", length = 100, nullable = false, unique = false)
      private String building;
  
      @Column(name="item_category", columnDefinition = "TEXT")
      private String category;
  
      @Column(name="item_visible", length = 1, nullable = false, unique = false)
      @Convert(converter= BooleanToStringConverter.class)
      private boolean visible;
  
      @Column(name="item_allowfree", length = 1, nullable = false, unique = false)
      @Convert(converter=BooleanToStringConverter.class)
      private boolean allowFree;
  
      @Column(name="item_allowreduced",length = 1, nullable = false, unique = false)
      @Convert(converter=BooleanToStringConverter.class)
      private boolean allowReduced;
  
      @Column(name="item_istypea",length = 1, nullable = false, unique = false)
      @Convert(converter=BooleanToStringConverter.class)
      private boolean typeA;
  
      @Column(name="item_reducedprice")
      private double reducedPrice;
  
      @Column(name="item_addby", length = 100, nullable = false, unique = false)
      private String addedBy;
  
      @Column(name="item_adddate")
      @Temporal(TemporalType.TIMESTAMP)
      private Date createDate;
  
      @Column(name="item_fr_bl")
      private int freeReducedBreakfastLunch;
  }
  </code></pre>
  
  <p>Here is my Service Tier:  </p>
  
  <pre><code>package com.cwssoft.pscafepos.services;
  
  import com.cwssoft.pscafepos.model.Item;
  import com.cwssoft.pscafepos.repositories.ItemRepository;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.util.Collections;
  import java.util.List;
  import java.util.Optional;
  
  @Service
  public class DefaultItemService extends BaseService&lt;Item, ItemRepository&gt; implements ItemService {
  
      public DefaultItemService(ItemRepository itemRepository) {
          super(itemRepository);
      }
  
      @Override
      @Transactional
      public Optional&lt;Item&gt; findByName(String name) {
          if (name != null) {
              return Optional.ofNullable(repository.findByName(name.trim()));
          }
          return Optional.empty();
      }
  
      @Override
      @Transactional
      public List&lt;Item&gt; findAll(String building) {
          if (building != null) {
              return repository.findByBuildingOrderByVisibleDescNameAsc(building.trim());
          }
          return Collections.emptyList();
      }
  
  }
  </code></pre>
  
  <p>and the service super class:</p>
  
  <pre><code>package com.cwssoft.pscafepos.services;
  
  import com.cwssoft.pscafepos.model.DomainObject;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.data.domain.Sort;
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.util.List;
  import java.util.Optional;
  
  @Slf4j
  public abstract class BaseService&lt;T extends DomainObject,U extends JpaRepository&lt;T,Long&gt;&gt; implements CrudService&lt;T,U&gt; {
  
      protected final U repository;
  
      public BaseService(U repository) {
          this.repository = repository;
      }
  
      public U getRepository() {
          return repository;
      }
  
      @Transactional
      public List&lt;T&gt; findAll() {
          return repository.findAll();
      }
  
      @Transactional
      public List&lt;T&gt; findAll(Sort sort) {
          return repository.findAll(sort);
      }
  
      @Transactional
      public List&lt;T&gt; findAllById(Iterable&lt;Long&gt; ids) {
          return repository.findAllById(ids);
      }
  
      @Transactional
      public Optional&lt;T&gt; save(T item) {
          return Optional.ofNullable(repository.saveAndFlush(item));
      }
  
      @Transactional
      public Optional&lt;T&gt; get(long id) {
          return Optional.ofNullable(prepare(repository.getOne(id)));
      }
  
      @Transactional
      public T prepare( T item ) {
          return item;
      }
  
      @Transactional
      public void delete(long id) {
          get(id).ifPresent( repository::delete );
      }
  }
  </code></pre>
  
  <p>And the controller:  </p>
  
  <pre><code>package com.cwssoft.pscafepos.controllers;
  
  import com.cwssoft.pscafepos.model.AdminUser;
  import com.cwssoft.pscafepos.model.Item;
  import com.cwssoft.pscafepos.services.AdminUserService;
  import com.cwssoft.pscafepos.services.ItemService;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.security.access.annotation.Secured;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.web.bind.annotation.*;
  
  import java.security.Principal;
  import java.util.Collections;
  import java.util.List;
  import java.util.Optional;
  import java.util.stream.Stream;
  
  @RestController
  @RequestMapping("/items")
  @Slf4j
  public class ItemController extends BaseController {
  
      private final ItemService service;
  
      public ItemController(ItemService itemService, AdminUserService adminUserService) {
          super(adminUserService);
          this.service = itemService;
      }
  
      @GetMapping("/all")
      @Secured("CAFE_MANAGER")
      public List&lt;Item&gt; all(Principal principal)  {
          return getLocation(principal)
                  .map( service::findAll )
                  .orElse( Collections.emptyList() );
      }
  
      @GetMapping("/single/{itemId}")
      @Secured("CAFE_MANAGER")
      public Item getSingle(Principal principal, @PathVariable("itemId") long id)  {
          final Optional&lt;String&gt; location = getLocation(principal);
  
          location
                  .flatMap( l -&gt; service.get(id) )
                  .ifPresent( item -&gt; {
                      log.info("Item.toString {}", item);
                  });
  
          return location
                  .flatMap( l -&gt; service.get(id) )
                  .filter( item -&gt; item.getBuilding() != null &amp;&amp; item.getBuilding().equalsIgnoreCase(location.orElse(null)) )
                  .orElse( null );
      }
  }
  </code></pre>
  
  <p>So what am I missing, why can't I pull back basic properties, there are no collections in the Item class and it about as simple as they come.  I see that I'm getting a HibernateProxy wrapped object back and when I attempt to call getBuilding is when it appears to be failing, even though building is just a simple string.  This is all before Jaskson is attempting to serialize the object to put it over the wire.  Thanks!</p>