Storing a hash as byte array with JPA Storing a hash as byte array with JPA database database

Storing a hash as byte array with JPA


I'm concerned that, since it doesn't know that it's a fixed length array (the length field of the Column annotation applies to strings only), (...)

If you specify the column length, Hibernate will use this information to determine the SQL column type to generate (TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB) though.

What I need is BINARY(32)

Did you try this?

@Column(columnDefinition="BINARY(32) NOT NULL")private byte[] passwordHash;


tinyblob is a good joice (mysql types reference), but all my apps work fine with Strings.If you really care about milli-seconds try both versions in a profiler and see what works best. My preferred profiler is the one included in netbeans.


As far as I know, a SHA-256 hash is always only printable characters (and if not, encode it base64), so the solution is that you CAN store it as string, then use the length field of the Column annotation.

Then you've got your fixed length and no doubt about performance.