title: JPA2 CriteriaBuilder: Using LOB property for greaterThan comparison
body: <p>My application is using SQLServer and JPA2 in the backend. App makes use of a timestamp column (<strong>in the SQLServer sense</strong>, which is equivalent to row version <a href="http://technet.microsoft.com/en-us/library/aa260631%28v=SQL.80%29.aspx" rel="nofollow">see here</a>) per entity to keep track of freshly modified entities. NB SQLServer stores this column as <strong>binary(8)</strong>. </p>
  
  <p>Each entity has a respective <code>timestamp</code> property, mapped as <code>@Lob</code>, which is the way to go for binary columns:</p>
  
  <pre><code>@Lob
  @Column(columnDefinition="timestamp", insertable=false, updatable=false)
  public byte[] getTimestamp() {
  ...
  </code></pre>
  
  <p>The server sends incremental updates to mobile clients along with the latest database timestamp. The mobile client will then pass the <em>old timestamp</em> back to the server on the next refresh request so that the server knows to return only fresh data. Here's what a typical query (in JPQL) looks like:</p>
  
  <pre><code>select v from Visit v where v.timestamp &gt; :oldTimestamp
  </code></pre>
  
  <p>Please note that I'm using a byte array as a query parameter and it works fine when implemented in JPQL this way.</p>
  
  <p>My problems begin when <strong>trying to do the same using the Criteria API</strong>:</p>
  
  <pre><code>private void getFreshVisits(byte[] oldVersion) {
    EntityManager em = getEntityManager();
    CriteriaQuery&lt;Visit&gt; cq = cb.createQuery(Visit.class);
    Root&lt;Visit&gt; root = cq.from(Visit.class);
    Predicate tsPred = cb.gt(root.get("timestamp").as(byte[].class), oldVersion); // compiler error
    cq.where(tsPred);
    ...
  }
  </code></pre>
  
  <p>The above will result in compiler error as it requires that the <code>gt</code> method used strictly with <code>Number</code>. One could instead use the <code>greaterThan</code> method which simply requires the params to be <code>Comparable</code> and that would result in yet another compiler error.</p>
  
  <p>So to sum it up, my question is: <strong>how can I use the criteria api to add a greaterThan predicate for a byte[] property</strong>? Any help will be greatly appreciated.</p>
  
  <p>PS. As to why I'm not using a regular DateTime <code>last_modified</code> column: because of concurrency and the way synchronization is implemented, this approach could result in lost updates. Microsoft's Sync Framework <a href="http://msdn.microsoft.com/en-us/library/cc305973.aspx" rel="nofollow">documentation</a> recommends the former approach as well.</p>