Brian B stumbled across a bit of code to generate UUIDs. Seeing that tag-line, I was worried that they invented their own UUID generator. The good news, is that they just use java.util.UUID
. The bad news is that they don’t understand how if
statements work.
public class UuidGenerator implements IdentifierGenerator {
@Value("${spring.profiles.active}")
private String profile;
@Resource
private Map<String, String> map;
@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
UUID id = UUID.randomUUID();
if(session.getFactory().getDialect() instanceof H2Dialect){
return UUID.randomUUID();
}
if( session.getFactory().getDialect() instanceof org.hibernate.dialect.PostgreSQLDialect ){
return id;
}
return id;
}
}
This class generates IDs so that Hibernate can properly store objects in the database. It starts by generating a randomUUID
. If, however, the type of database they’re talking to uses the H2Dialect
, they’ll… generate an entirely new UUID and return that. If, on the other hand, it’s a Postgres database, they’ll… return the original UUID. If it’s none of those cases… it returns the original UUID.
There are other problems in this code- it loads a profile object from the config file, but never uses it. It creates a map
which it never uses.
This code was written in 2016. It hasn’t been touched since. Only one developer has ever touched it, and perhaps they had grand plans for a complex ID generator, or perhaps they just blindly copy/pasted code which they didn’t understand.
Brian isn’t allowed to touch it, or to remove it. The entire module it’s part of works and no one is entirely certain why. So there it lives, forever and ever.