title: JPA: How to select a binary(16) UUID by a String UUID?
body: <p>I have the following Entity:</p>
  <pre><code>@Data
  @Entity
  public class Comment implements Serializable {
  
      @Id
      @GeneratedValue(generator = &quot;uuid4&quot;)
      @GenericGenerator(name = &quot;UUID&quot;, strategy = &quot;uuid4&quot;)
      @Column(columnDefinition = &quot;BINARY(16)&quot;)
      private UUID id;
  
      @Column(columnDefinition = &quot;BINARY(16)&quot;)
      private UUID imageId;
  
      private Instant creationTime;
  
      private String text;
  }
  </code></pre>
  <p>And a CRUD repo:</p>
  <pre><code>public interface CommentsRepository extends CrudRepository&lt;Comment, UUID&gt; {
  
      List&lt;Comment&gt; findAllByImageId(final UUID imageId);
  }
  </code></pre>
  <p>I add some example data:</p>
  <pre><code>@Component
  @Slf4j
  public class CommentsSampleData implements CommandLineRunner {
  
      private final CommentsRepository repository;
  
      @Autowired
      public CommentsSampleData(final CommentsRepository repository) {
          this.repository = repository;
      }
  
      @Override
      public void run(String... args) {
          createComment(&quot;617220ff-1642-4490-b589-869e7978c5e0&quot;, Instant.now(), &quot;comment1&quot;);
          createComment(&quot;617220ff-1642-4490-b589-869e7978c5e0&quot;, Instant.now(), &quot;comment2&quot;);
          createComment(&quot;617220ff-1642-4490-b589-869e7978c5e0&quot;, Instant.now(), &quot;comment3&quot;);
          createComment(&quot;e3a8aa57-6937-4f9e-b117-78bafe61b718&quot;, Instant.now(), &quot;comment1&quot;);
      }
  
      private void createComment(
              final String imageId,
              final Instant creationTime,
              final String text) {
          final Comment comment = new Comment();
          comment.setImageId(UUID.fromString(imageId));
          comment.setCreationTime(creationTime);
          comment.setText(text);
  
          log.info(&quot;save comment: {}&quot;, comment);
  
          repository.save(comment);
      }
  }
  </code></pre>
  <p>So the data in my table looks like the following:</p>
  <p><a href="https://i.stack.imgur.com/l11Qi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l11Qi.png" alt="enter image description here" /></a></p>
  <p>So what is the best way now to select by those binary UUID's?
  I will get string UUID's from the frontend So I guess I somehow need to convert those Strings to Binaryies. Whats the best way to do so so that it also works with ids and primary keys.</p>
  <p>Example endpoint:</p>
  <pre><code>@Slf4j
  @RestController
  public class CommentsController {
  
      private final CommentsService service;
  
      public CommentsController(final CommentsService service) {
          this.service = service;
      }
  
      @GetMapping(value = &quot;/comments&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
      public List&lt;Comment&gt; getComments(@RequestParam(&quot;imageId&quot;) final UUID imageId) {
          log.info(&quot;get comments by imageId: {}&quot;, imageId);
  
          String existingIds = service.findAll().stream()
                  .map(Comment::getImageId)
                  .map(UUID::toString)
                  .collect(Collectors.joining(&quot;,&quot;));
  
          log.info(&quot;Image Id Passed: {}&quot;, imageId);
          log.info(&quot;Existing image ids: {}&quot;, existingIds);
  
          String resultIds = service.findAllByImageId(imageId).stream()
                  .map(Comment::getImageId)
                  .map(UUID::toString)
                  .collect(Collectors.joining(&quot;,&quot;));
  
          log.info(&quot;Result image ids: {}&quot;, resultIds);
  
          return service.findAllByImageId(imageId);
      }
  }
  </code></pre>
  <p>When I now do a request:</p>
  <pre><code>localhost:8080/comments?imageId=617220ff-1642-4490-b589-869e7978c5e0
  </code></pre>
  <p>I get no result even though the UUID exists but not as string, it exists as binary(16) in the database:</p>
  <pre><code>d.f.a.c.service.CommentsController       : Image Id Passed: 617220ff-1642-4490-b589-869e7978c5e0
  d.f.a.c.service.CommentsController       : Existing image ids: 617220ff-1642-4490-b589-869e7978c5e0,617220ff-1642-4490-b589-869e7978c5e0,617220ff-1642-4490-b589-869e7978c5e0,e3a8aa57-6937-4f9e-b117-78bafe61b718
  d.f.a.c.service.CommentsController       : Result image ids:
  
  </code></pre>