Last active
July 18, 2020 21:49
-
-
Save ehabqadah/a24bff99b19a6fc3f3bec81010623911 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A custom Id generator based on combination of long time hex string and UUID | |
* | |
* @author Ehab Qadah | |
*/ | |
public class BaseIdentifierGenerator extends UUIDGenerator { | |
private static final int NUMBER_OF_CHARS_IN_ID_PART = -5; | |
@Override | |
public Serializable generate(SharedSessionContractImplementor session, Object obj) throws HibernateException { | |
// Generate a custom ID for the new entity | |
final String uuid = super.generate(session, obj).toString(); | |
final long longTimeRandom = System.nanoTime() + System.currentTimeMillis() | |
+ new Random().nextLong() + Objects.hash(obj); | |
final String timeHex = Long.toHexString(longTimeRandom); | |
return StringUtils.substring(timeHex, NUMBER_OF_CHARS_IN_ID_PART) | |
+ StringUtils.substring(uuid, NUMBER_OF_CHARS_IN_ID_PART); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment