title: How to get the value of the lazy Hibernate field through reflection
body: <p>I found similar questions, but they did not answer my question.</p>
  
  <p>I have two entities with a many-to-one relationship - unidirectional. 
  But most importantly, the relationship is lazy. Because it is correct to use a lazy connection, everyone knows it.</p>
  
  <p>Code:</p>
  
  <pre><code>@Entity
  public class User implements BaseEntity {
  
      @Id
      @Column
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column
      private String name;
  
      @ManyToOne(fetch = FetchType.LAZY)
      private City city;
  
  }
  
  @Entity
  public class City implements BaseEntity {
  
      @Id
      @Column
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column
      private String name;
  }
  
  interface BaseEntity {
      void setId(Long id);
      Long getId();
  }
  </code></pre>
  
  <p>I wrote a method that allows you to search by the transferred fields of the entity.
  An example of how this works:</p>
  
  <pre><code>public class Search&lt;T extends BaseEntity&gt; {
  
      public List&lt;T&gt; getByFields(T entity, List&lt;FieldHolder&gt; data) {
          // create criteria with passed field name and value by reflection
      } 
  }
  
  class FieldHolder {
      private String fieldName;
      private Object value;
      /**
       * "true" - means that the field "value" contains id related object
       * "false" - elementary type like: String, Wrapper, Primitive
       */
      private boolean isRelationId;
  }
  </code></pre>
  
  <p>The problem is that problems start when you need to search and related objects - by creating related queries.</p>
  
  <p>The following entry is used to send the associated field: "city.id" and the problem is that when I transfer the essence of the related object (City) it is in a proxy and I cannot get id by reflection from City.</p>
  
  <p>My function works perfectly if you specify:</p>
  
  <pre><code>@ManyToOne(fetch = FetchType.EAGER)
  private City city;
  </code></pre>
  
  <p>But it will greatly affect performance, since I have a lot of related objects. Therefore, I want to solve this problem for a lazy load.</p>
  
  <p>I know that this is not an easy task. But perhaps there is some opportunity to somehow get around this problem.</p>