Hibernate 提供了Integrator
可用于此的接口。例如:
public class CustomUserTypesIntegrator implements Integrator {
public void integrate(Configuration configuration,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
// Register the custom user type mapping(s) with Hibernate.
CustomUserType customUserType = new CustomUserType();
configuration.registerTypeOverride(customUserType,
new String[] { customUserType.returnedClass().getName() });
}
public void integrate(MetadataImplementor metadata,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
// Nothing to do here.
}
public void disintegrate(SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
// Nothing to do here.
}
}
然后需要通过 Java 的标准 SPI 机制将其公开为服务提供者来注册。例如:
my-project/
src/main/resources/
META-INF/
services/
org.hibernate.integrator.spi.Integrator
其中将包含以下内容:
com.example.CustomUserTypesIntegrator
对于这方面的参考,我建议查看Jadira 用户类型库是如何做到的:https ://github.com/JadiraOrg/jadira/ 。特别是,我发现他们的AbstractUserTypeHibernateIntegrator
课程值得一看。