CodeSOD: Identify Yourself

This post was originally published on this site

The Daily WTF

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

To read the full article click on the 'post' link at the top.