休眠5.0 +春分4.5.0和自定义UserType

问题描述 投票:1回答:1

我能够在非托管JPA模式下与Equinox 4.5.0一起使用Hibernate 5.0 Osgi Bundle。但是,有一个问题,当我在其中一个实体中使用自定义UserType(例如实现了EnhancedUserType的TestTypeMapType)时,出现以下异常:

org.hibernate.MappingException: Could not determine type for: com.xxx.yyy.TestTypeMapType, at table: TestTable, for columns: [org.hibernate.mapping.Column(testType)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:390)

当我从Hibernate来源检查SimpleValue.java时,我看到它正在使用org.hibernate.internal.util.Reflecthelper通过下面的代码行创建类型信息

ReflectHelper.classForName(typeName);

并且ReflectHelper使用以下几行创建类型信息

try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        return classLoader.loadClass(typeName);
    }
}
catch(Throwable ignore){}

return Class.forName(typeName);

classLoader.loadClass和Class.forName都抛出以下异常

java.lang.ClassNotFoundException: com.xxx.yyy.TestTypeMapType cannot be found by org.hibernate.core_5.0.2.Final

但是如果我直接从创建EntityManagerFactory的包中执行ReflectHelper.classForName内容,则可以成功创建自定义UserType的类型信息。专门针对以下示例,行A和行B有效,但行C抛出异常。

try {
    Class<?> typeClass = null;

    // A
    typeClass = Thread.currentThread().getContextClassLoader().loadClass("com.xxx.yyy.TestTypeMapType");

    // B
    typeClass = Class.forName("com.xxx.yyy.TestTypeMapType");

    // C
    typeClass = ReflectHelper.classForName("com.xxx.yyy.TestTypeMapType");
}
catch(Exception e){}

我该怎么办,以便我的自定义UserType可以在osgi环境中被hibernate发现?

PS:我对其他实体类,方言,JDBC驱动程序等没有任何问题,并且不建议使用ReflectHelper.classForName,建议使用ClassLoaderService或ClassLoaderAccess。可以是休眠错误吗?

谢谢...

hibernate jpa osgi equinox compositeusertype
1个回答
0
投票

您需要按此处所述使用TypeContributor扩展点。

© www.soinside.com 2019 - 2024. All rights reserved.