Search code examples
spring-boothibernatehashsha256composite-key

Composite key with EmbeddedId vs calculated hash id with sha256 on hibernate spring boot and postgresql


I use java spring boot and hibernate with postgresql. I need a composite key (embeddedId) because an id is made by a composition of 3 other IDs. Is it better to calculate by sha-256 a single id or use a composite key for this entity? I need the best performance and I think a find on a single key is faster than a find on an id made by 3 id. Is the result of the sha-256 calculation repeatable (spring hashcode depend on the execution instance)?

Thanks


Solution

  • I don't know what data types these 3 columns have, but if they are ints or longs you would only have 32 * 3 = 96 or 64 * 3 = 192 bits to store and compare for lookups which is usually faster than 256 bits. Apart from that, you can theoretically have hash collisions for different values, so the hash is not necessarily unique. There are articles and posts out there suggesting that SHA-256 is pretty unique, but I wouldn't bet on it if this is really critical.