I'm trying to find the best solution for the given problem:
I have an Entity (lets call it Collateral
) which consists of several fields. The uniqueness of this entity is defined by the composition of 4 fields (let's call them: user_id (bigint)
, device_id(varcha(9))
, key_id(varchar(2732))
, application_id(varchar(255))
)
This table is being generated with hibernate. I have tried both moving this 4 fields to a separate entity (CollateralEmbeddedEntity
) to use it as embedded Id, and creating the constraint on those 4 fields within Collateral
Entity:
@Table(
name="COLLATERAL",
uniqueConstraints=
@UniqueConstraint(name = "comp_key", columnNames={"device_id", "application_id", "key_id", "user_id"}))
Problem is that in both cases, the fields altogether exceed the maximum allowed length of the MariaDB key:
java.sql.SQLException: Specified key was too long; max key length is 3072 bytes
Changing the dbCharset encoding (collation), or shrinking the fields varchar range itself is not an option.
What I have thought about is to generate and store a hash of those 4 fields and give it a unique constraint (search and update will always be based on these 4 fields all together anyway) however, I'm not sure if such solution is appropriate since we are violating the database normalization with redundant information.
Is the solution with hash is actually a good one? If not, what are the better alternatives for the given problem?