As long as you already have the ID in binary format, the request is simple:
byte[] id = ....; em.createQuery("SELECT x FROM TableName x WHERE x.id = ?1″, TableName.class).setParameter(1, id).getSingleResult();
In fact, if you are just looking at the primary key, you can use
em.find(TableName.class, id);
Getting the identifier in binary format can be a little painful, especially if you need to pass it to URLs, etc. I recommend encoding / decoding Base64; Apache Commons Codec has helper methods for moving from bytes [] to a string protected by a URL and then back to bytes []
source share