首页 > 解决方案 > 如何以编程方式为选定的实体字段绑定休眠类型?

问题描述

我正在寻找一种Type在实体管理器配置阶段绑定特定实体字段的方法。我需要它能够使用外部源将额外的“规则”应用于目标实体字段,而无需更改实体类。

所以基本上我试图避免硬编码@Type注释方式如下:

@Type(type = foo.package.MyType, parameters = {
    @Parameter(name = "fooProperty", value = "fooValue")
})
private String someField;

相反,我想在以someField编程方式构建模型时设置类型。

标签: javahibernatehibernate-mapping

解决方案


这是我以前见过的一种方式。它有点低级,所以我怀疑有一种更清洁的方法可以做到这一点。

这使用了PersisterHibernate 中的自定义来允许我们在创建SessionFactory( EntityManagerFactory) 时替换类型。

首先,@Persister注解用于声明自定义Persister

@Entity
@Persister(impl = MyPersister.class)
public class EntityWithPersister {

    private String someField;

那么通常自定义持久化器应该SingleTableEntityPersister在 Hibernate 中扩展。如果实体使用不同的@Inheritance(strategy),那么它可能需要扩展JoinedSubclassEntityPersisterUnionSubclassEntityPersister代替。

这提供了在构造点更改类型的机会,例如:

public class MyPersister extends SingleTableEntityPersister {

    public MyPersister(PersistentClass persistentClass,
            EntityDataAccess cacheAccessStrategy,
            NaturalIdDataAccess naturalIdRegionAccessStrategy,
            PersisterCreationContext creationContext)
            throws HibernateException {
        super(modify(persistentClass), cacheAccessStrategy,
                naturalIdRegionAccessStrategy, creationContext);
    }

    private static PersistentClass modify(PersistentClass persistentClass) {
        SimpleValue value = (SimpleValue) persistentClass
                .getProperty("someField").getValue();
        value.setTypeName(MyType.class.getName());
        return persistentClass;
    }
}

如果您需要访问更多您所处的上下文,creationContext.getSessionFactory()这可能是一个很好的起点。


推荐阅读