顯然它可能是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";
}
}
我的問題是,休眠似乎並不認識它,這是值得注意的是,在一個點上我做了「UUID定製」,在類型的靜態字符串,並直接在@Type
引用,所以它的不喜歡它實際上不在類路徑中。
引起:org.hibernate.MappingException:在org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:510) 在org.hibernate作爲UUID定製 :無法確定類型。 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) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryB uilderImpl.java:850) ... 45更多 原因:org.hibernate.annotations.common.reflection.ClassLoadingException:無法加載Class [uuid-custom] at org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl .classForName(StandardClassLoaderDelegateImpl.java:60) 在org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:491) ...... 50多個 造成的:拋出java.lang.ClassNotFoundException:UUID定製 在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) 在java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308) 在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)
我還需要做些什麼來實現這個目標?