I can definitely see the benefit of this in principal. This difficulty is the bit you gloss over though
I will say that this is something that can be done today. Have a look at org.hibernate.dialect.Dialect#contributeTypes. This approach ties this resolution to the Dialect itself. The problem with this approach is that it does not work well for user-supplied types, without also having user-supplied dialects.
I think that a solution that is external from both Type and Dialect is best. And in fact, this is in place today as well. You should be able to achieve this using org.hibernate.metamodel.spi.TypeContributor. Something like:
class TypeContributorImpl implements TypeContributor {
@Override
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
final Dialect dialect = serviceRegistry.getService( JdbcServices.class ).getDialect();
if ( isPostgres( dialect ) ) {
typeContributions.contributeType( new PGFoo() );
}
else if ( isMySql( dialect ) ) {
typeContributions.contributeType( new MySqlFoo() );
}
...
}
}
-
On a side note, I recently refactored away this org.hibernate.metamodel package. TypeContributor, for example, will be org.hibernate.boot.model.TypeContributor in 5.0. Just FYI.
|