title: Hibernate not fetching from Second Level Cache if ID column is VARCHAR
body: <p>I am trying Spring Boot and Hibernate Second Level Caching. I was successful in getting it to work. However, I am stuck with a strange scenario where entities are not fetched from the second level cache if the ID column is a VARCHAR. Here are the relevant classes:</p>
  
  <pre><code>@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
  @Table(name = "user1")
  @Entity
  public class User {
      private long userId;
      private String name;
  
      @Id
      @Column(name = "id")
      public long getUserId() {
          return userId;
      }
  
      public void setUserId(long userId) {
          this.userId = userId;
      }
  
      @Column(name = "name")
      public String getName() {
          return name;
      } 
  
      public void setName(String name) {
          this.name = name;
      }
  
      public User(String name) {
          this.name = name;
      }
  
      public User() {
      }
  }
  </code></pre>
  
  <p>This Entity works fine, here are the debug logs from hibernate for first and second fetch</p>
  
  <p><strong>Fetch 1</strong></p>
  
  <pre><code>o.h.e.t.internal.TransactionImpl         : begin
  org.hibernate.SQL                        : select user0_.id as id1_1_0_,     user0_.name as name2_1_0_ from user1 user0_ where user0_.id=?
  o.h.l.p.e.p.i.ResultSetProcessorImpl     : Starting ResultSet row #0
  l.p.e.p.i.EntityReferenceInitializerImpl : On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified
  o.h.engine.internal.TwoPhaseLoad         : Resolving associations for  [com.voyant.master.entities.User#1]
  o.h.engine.internal.TwoPhaseLoad         : Adding entity to second-level cache: [com.voyant.master.entities.User#1]
  o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.voyant.master.entities.User#1]
  o.h.r.j.i.ResourceRegistryStandardImpl   : HHH000387: ResultSet's statement was not registered
  .l.e.p.AbstractLoadPlanBasedEntityLoader : Done entity load : com.voyant.master.entities.User#1
  o.h.e.t.internal.TransactionImpl         : committing
  </code></pre>
  
  <p><strong>Fetch 2</strong></p>
  
  <pre><code>o.h.e.t.internal.TransactionImpl         : begin
  o.h.e.t.internal.TransactionImpl         : committing
  </code></pre>
  
  <p>Now, the problematic Entity definition:</p>
  
  <pre><code>@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
  @Table(name = "user")
  @Entity
  public class User2 {
  
      @Id
      @Column(name = "username")
      private String username;
  
      @Column(name = "lastname")
      private String lastname;
  
      @Column(name = "name")
      private String name;
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getLastname() {
          return lastname;
      }  
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public User2(String name) {
          this.name = name;
      }
  
      public User2() {
      } 
  }
  </code></pre>
  
  <p>Hibernate debug logs from two consecutive fetch of same entity</p>
  
  <p><strong>Fetch 1</strong></p>
  
  <pre><code>o.h.e.t.internal.TransactionImpl         : begin
  o.h.jpa.criteria.CriteriaQueryImpl       : Rendered criteria query -&gt; select generatedAlias0 from User2 as generatedAlias0 where generatedAlias0.username=:param0
  org.hibernate.SQL                        : select user2x0_.username as username1_0_, user2x0_.lastname as lastname2_0_, user2x0_.name as name3_0_ from user user2x0_ where user2x0_.username=?
  org.hibernate.loader.Loader              : Result set row: 0
  org.hibernate.loader.Loader              : Result row: EntityKey[com.voyant.master.entities.User2#dharam]
  o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.voyant.master.entities.User2#dharam]
  o.h.engine.internal.TwoPhaseLoad         : Adding entity to second-level cache: [com.voyant.master.entities.User2#dharam]
  o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.voyant.master.entities.User2#dharam]
  o.h.e.t.internal.TransactionImpl         : committing
  o.h.e.i.AbstractFlushingEventListener    : Processing flush-time cascades
  o.h.e.i.AbstractFlushingEventListener    : Dirty checking collections
  o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
  o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
  o.hibernate.internal.util.EntityPrinter  : Listing entities:
  o.hibernate.internal.util.EntityPrinter  : com.voyant.master.entities.User2{name=dharam, username=dharam, lastname=dharam}
  </code></pre>
  
  <p><strong>Fetch 2</strong></p>
  
  <pre><code>o.h.e.t.internal.TransactionImpl         : begin
  o.h.jpa.criteria.CriteriaQueryImpl       : Rendered criteria query -&gt; select generatedAlias0 from User2 as generatedAlias0 where generatedAlias0.username=:param0
  org.hibernate.SQL                        : select user2x0_.username as username1_0_, user2x0_.lastname as lastname2_0_, user2x0_.name as name3_0_ from user user2x0_ where user2x0_.username=?
  org.hibernate.loader.Loader              : Result set row: 0
  org.hibernate.loader.Loader              : Result row: EntityKey[com.voyant.master.entities.User2#dharam]
  o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.voyant.master.entities.User2#dharam]
  o.h.engine.internal.TwoPhaseLoad         : Adding entity to second-level cache: [com.voyant.master.entities.User2#dharam]
  o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.voyant.master.entities.User2#dharam]
  o.h.e.t.internal.TransactionImpl         : committing
  o.h.e.i.AbstractFlushingEventListener    : Processing flush-time cascades
  o.h.e.i.AbstractFlushingEventListener    : Dirty checking collections
  o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
  o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
  o.hibernate.internal.util.EntityPrinter  : Listing entities:
  o.hibernate.internal.util.EntityPrinter  : com.voyant.master.entities.User2{name=dharam, username=dharam, lastname=dharam}
  </code></pre>