Apparently it's possible to create a TypeDef that can switch implement implementations based on dialect.
package org.hibernate.type;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.UUIDTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;
import java.io.IOException;
import java.util.Properties;
public class UUIDCustomType extends AbstractSingleColumnStandardBasicType {
private static final long serialVersionUID = 902830399800029445L;
private static final SqlTypeDescriptor SQL_DESCRIPTOR;
private static final JavaTypeDescriptor TYPE_DESCRIPTOR;
static {
Properties properties = new Properties();
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
properties.load( loader.getResourceAsStream( "database.properties" ) );
}
catch ( IOException e ) {
throw new RuntimeException( "Could not load properties!", e );
}
String dialect = properties.getProperty( "dialect" );
if ( dialect.equals( "org.hibernate.dialect.PostgreSQLDialect" ) ) {
SQL_DESCRIPTOR = PostgresUUIDType.PostgresUUIDSqlTypeDescriptor.INSTANCE;
} else if(dialect.equals("org.hibernate.dialect.H2Dialect")) {
SQL_DESCRIPTOR = VarcharTypeDescriptor.INSTANCE;
} else {
throw new UnsupportedOperationException("Unsupported database!");
}
TYPE_DESCRIPTOR = UUIDTypeDescriptor.INSTANCE;
}
public UUIDCustomType() {
super(SQL_DESCRIPTOR, TYPE_DESCRIPTOR);
}
@Override
public String getName() {
return "uuid-custom";
}
}
my problem is that hibernate doesn't seem to recognize it, it's worth noting that at one point I made "uuid-custom" a static string in the type and referenced that directly in the @Type
, so it's not like it's not actually on the classpath.
Caused by: org.hibernate.MappingException: Could not determine type for: uuid-custom at org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:510) at org.hibernate.cfg.SetSimpleValueTypeSecondPass.doSecondPass(SetSimpleValueTypeSecondPass.java:42) at org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1470) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1418) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ... 45 more Caused by: org.hibernate.annotations.common.reflection.ClassLoadingException: Unable to load Class [uuid-custom] at org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl.classForName(StandardClassLoaderDelegateImpl.java:60) at org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:491) ... 50 more Caused by: java.lang.ClassNotFoundException: uuid-custom at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:344) at org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl.classForName(StandardClassLoaderDelegateImpl.java:57)
what else do I need to do to get this to work?