views:

100

answers:

3

My User entity class contains password hash field, which is a byte array with a fixed length (32 since it's an SHA-256 hash).

@Entity
public class User {
    @Column(nullable=false)
    private byte[] passwordHash;
    ...
}

As you can see, I haven't annotated it with anything special, just a NOT NULL.

This works, but will it perform? My schema is generated by Hibernate, but I don't know exactly what it generates (I'm currently using an in-memory HSQL database).

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), it will store this hash in a BLOB field which is added in the record as a pointer (if I understand correctly how databases work).

Is this true, and how can I change this? Should I just encode the hash as a string, with base64 or hex, accepting the small performance/correctness impact of that?

+1  A: 

Hi Bart, 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.

klause
A: 

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.

lImbus
Nope, I've seen the output and it really consists mostly of unprintable characters (or rather isn't character data at all). I could of course base64 or hex encode it, but if I could store it in pure binary form, that would be nice.
Bart van Heukelom
+2  A: 

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;
Pascal Thivent