title: Java JPA Preventing Proxies from calling db
body: <p>I have a spring boot (1.5.4.RELEASE) project using Java 8. I have an entity and it's related domain class like this:</p>
  
  <pre><code>@Entity
  @Table(name = "Foo", schema = "dbo")
  public class FooEntity implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "Id")
      private int id;
  
      @Column(name="Name")
      private String name;
  
      @Column(name="Type")
      private String type;
  
      @Column(name="Color")
      private String color;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "Car")
      private Car car;
  
      //getter and setter
  }
  
  public class Foo {
      private int id;
      private String name;
      private String type;
      private String color;
      private Car car;
  
      //Constructors and getters
  }
  </code></pre>
  
  <p>I want to create a repository that fetches this Foo object from the DB but only fetching the complex fields if the user asks for them to prevent unnecessary join statements. The repo looks like this:</p>
  
  <pre><code>import static com.test.entities.QFooEntity.fooEntity;
  import static com.test.entities.QCarEntity.carEntity;
  
  @Repository
  public class FooRepository {
      private final JPAQuery&lt;FooEntity&gt; query = createQuery().from(fooEntity);
  
      public FooRepository getFooByName(String name) {
          query.where(fooEntity.name.eq(name));
          return this;
      }
  
      public FooRepository withCar() {
          query.leftJoin(fooEntity.car, carEntity).fetchJoin();
          return this;
      }
  
      public Foo fetch() {
          FooEntity entity = query.fetchOne();
          return FooMapper.mapEntityToDomain().apply(entity);
      }
  }
  </code></pre>
  
  <p>So a barebones call for a Foo object will return the Entity with values for all the fields except for the car field. If the user wants car information then they have to explicitly call <code>withCar</code>. </p>
  
  <p>Here is the mapper:</p>
  
  <pre><code>public class FooMapper {
      public static Function&lt;FooEntity, Foo&gt; mapEntityToDomain() {
          return entity -&gt; { 
              return new Foo(e.getId(), e.getName(), e.getType(), e.getColor(), e.getCar());
          };
      }
  }
  </code></pre>
  
  <p>The problem is when you do <code>e.getCar()</code> if the value is not there (i.e. there's a proxy present) JPA will go out and fetch it for you. I don't want this to be the case. It will just grab the values and map them to the domain equivalent if it's not there then <code>null</code>. </p>
  
  <p>One solution that I've heard (and tried) is calling <code>em.detach(entity);</code> however, this doesn't work as I intended because it throws an exception when you try to access <code>getCar</code> and I've also heard this is not best practice. </p>
  
  <p>So my question is what is the best way to create a repo using a builder pattern on a JPA entity and not have it call the DB when trying to map. </p>