显然可以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";
}
}
我的问题是,hibernate似乎并没有意识到它,值得注意的是,我曾经做过" uuid-custom"类型中的静态字符串,并直接在@Type
中引用,因此它不像它实际上不在类路径中。
引起:org.hibernate.MappingException:无法确定类型:uuid-custom 在org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:510) at org.hibernate.cfg.SetSimpleValueTypeSecondPass.doSecondPass(SetSimpleValueTypeSecondPass.java:42) 在org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1470) 在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1418) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:850) ......还有45个 引起:org.hibernate.annotations.common.reflection.ClassLoadingException:无法加载Class [uuid-custom] 在org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl.classForName(StandardClassLoaderDelegateImpl.java:60) 在org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:491) ......还有50个 引起:java.lang.ClassNotFoundException:uuid-custom 在java.net.URLClassLoader $ 1.run(URLClassLoader.java:372) 在java.net.URLClassLoader $ 1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) 在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) 在org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl.classForName(StandardClassLoaderDelegateImpl.java:57)
我还需要做些什么才能让它发挥作用?
答案 0 :(得分:1)
我不确定是否有办法让它工作,否则将其添加到package-info.java
中的typedef修复问题
@TypeDef(
name = UUIDCustomType.UUID,
defaultForType = UUID.class,
typeClass = UUIDCustomType.class
)
package com.xenoterracide.rpf.model;
import org.hibernate.annotations.TypeDef;
import org.hibernate.type.UUIDCustomType;
import java.util.UUID;