title: Using a UUID as a Database Primary Key, Java type is a byte[]
body: <p>Are there any issues with using a byte[] as a primary key in a JPA Entity?</p>
  
  <p>I want to use a UUID as my primary key, but stored as a string I feel it will be too large. </p>
  
  <p>I was thinking of doing something like this to store the ID as a byte[] and set it as my Entity's ID:</p>
  
  <pre><code>    public static byte[] byteArray(UUID uuid) {
          long lsb = uuid.getLeastSignificantBits();
          long msb = uuid.getMostSignificantBits();
  
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          DataOutputStream dos = new DataOutputStream(bos);
          try {
              dos.writeLong(lsb);
              dos.writeLong(msb);
              dos.flush();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          byte[] data = bos.toByteArray();
  //      System.out.println("Byte Array Length "+data.length);
          return data;
  
      }
  </code></pre>
  
  <p>Will I have any trouble putting indexes on this in the DB?  I am using both Postgres and HSQL.  I am using Hibernate as my JPA provider.</p>