如何使用org.springframework.orm.hibernate4.LocalSessionFactoryBean在Hibernate中注册自定义类型

时间:2012-11-17 11:05:15

标签: spring

我正在从hibernate 3迁移到hibernate 4, 在hibernate 3中,我注册自定义类型的方式是:

public class AnnotationSessionFactoryBean extends org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean {
private Collection<? extends BasicType> customTypes;

public Collection<? extends BasicType> getCustomTypes() {
    return customTypes;
}

public void setCustomTypes(Collection<? extends BasicType> customTypes) {
    this.customTypes = customTypes;
}

@Override
protected Configuration newConfiguration() throws HibernateException {
    Configuration configuration = super.newConfiguration();
    if (CollectionUtils.hasEntries(customTypes)) {
        for (BasicType customType:customTypes) {
            configuration.registerTypeOverride(customType);
        }
    }
    return configuration;
}}

我现在正在尝试使用hibernate 4进行相同的操作,我的问题是如何做到这一点的最佳方法?因为我没有访问权限,所以在使用“org.springframework.orm.hibernate4.LocalSessionFactoryBean”而不是“org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”时更改配置。

由于

2 个答案:

答案 0 :(得分:3)

在您的一个实体类中,按如下方式注释该类:

@TypeDefs({
  @TypeDef(name="type1",typeClass=me.sample.Type1.class),
  @TypeDef(name="type2",typeClass=me.sample.Type2.class)
})

Spring 3.1将AnnotationSessionFactoryBean与LocalSessionFactoryBean合并,委托Hibernate配置是否使用注释与XML连接。

答案 1 :(得分:3)

在Hibernate 4中,您可以使用LocalSessionFactoryBean而无需扩展它:

sessionFactory.getConfiguration().registerTypeOverride(TypeOverride.INSTANCE);

虽然,这将在Hibernate 5.0中消失:

注意:在4.0版本之后,将使用org.hibernate.boot.registry.StandardServiceRegistryBuilder和org.hibernate.metamodel.MetadataSources替换此版本,此时此类将被弃用并计划在5.0中删除。有关详细信息,请参见HHH-6183,HHH-2578和HHH-6586