在运行时动态向Hibernate实体添加字段

3
我有一个带有自定义字段的应用程序 - 用户可以通过选择字段类型和命名来定义自定义字段。然后,这些自定义字段被呈现为实体的一部分,并且给定到这些字段的数据保存到我的数据库中。在大多数情况下,我已经能够通过编程和正常的Hibernate映射(即@OneToMany注释集合)处理它们而没有问题。然而,我目前面临一个问题。我们想要将这些自定义字段及其值用于“父”实体的实时报告。自定义字段值被映射为父实体内的集合,但我需要它们为了报告目的而是扁平化的。我创建了一个视图,从SQL方面提供了我所需的内容 - 我按照这个示例添加了动态透视,生成的查询正是我想要显示信息的方式。当然不包括以下图片,但基本上这就是我拥有的输出。

pivot1

pivot2

该视图返回完全动态的列数,每个列都以自定义字段命名,并填充具有相关数据的行。

问题是我现在不知道如何使用Hibernate检索此信息。

我找到了有关通过从Hibernate配置获取ClassMappings更新PersistentClass的文档:

在运行时操作元数据

//Get the existing mapping for AgreementsGrid from Configuration
PersistentClass gridMapping = configuration.getClassMapping(AgreementsGrid.class.getName());

//Define new Column
Column column = new Column();
column.setName("ESTIMATED_COST_OVERRUNS");
column.setNullable(true);
column.setUnique(false);
gridMapping.getTable().addColumn(column);

//Wrap the column in a value
SimpleValue value = new SimpleValue();
value.setTable(gridMapping.getTable());
value.setTypeName("string");
value.addColumn(column);

//Define new property for the AgreementsGrid class
Property prop = new Property();
prop.setValue(value);
prop.setName("customField1");
prop.setNodeName(prop.getName());
gridMapping.addProperty(prop);

//Build a new session factory for the new mapping
SessionFactory sessionFactory = configuration.buildSessionFactory();

我刚刚意识到这是针对Hibernate 3和4的,甚至在Hibernate 5中都不可能(我正在使用5.2.18)。
因此,我正试图弄清楚如何在Hibernate 5中处理此问题。我有一个基本实体映射到视图,运行时我需要能够动态地向其添加“字段”,以便我的DAO可以动态地过滤信息并处理排序/分组。
这是我为我的视图准备的实体:
@Entity
@Table(name="AGREEMENTS_GRID")
public class AgreementsGrid implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer entityId;
    @Column(name="ENTITY_ID")
    @Id
    public Integer getEntityId() {
        return this.entityId;
    }
    public void setEntityId(Integer entityId) {
        this.entityId = entityId;
    }

    private Agreements agreement;
    @ManyToOne
    @JoinColumn(name = "AGREEMENT_ID", referencedColumnName = "ID", nullable = false)
    public Agreements getAgreement() {
        return this.agreement;
    }
    public void setAgreement(Agreements agreement) {
        this.agreement= agreement;
    }

    private BigDecimal expenditure;
    @Column(name = "EXPENDITURE", nullable = true, precision = 22, scale = 2)
    public BigDecimal getExpenditure() {
        return this.expenditure;
    }
    public void setExpenditure(BigDecimal expenditure) {
        this.expenditure = expenditure;
    }

    /*
     * Dynamic fields would theoretically go here and look like this,
     * for a custom field of type CURRENCY named 'Estimated Cost Overruns'
     */
    /*
    private BigDecimal customField1;
    @Column(name = "ESTIMATED_COST_OVERRUNS", nullable = true, precision = 22, scale = 2)
    public BigDecimal getCustomField1() {
        return this.customField1;
    }
    public void setCustomField1(BigDecimal customField1) {
        this.customField1 = customField1;
    }
    */

 }

为了明确,我无法在编译时映射这些字段。它们完全是自定义的,并且完全由用户定义。在运行时,我将能够知道存在哪些自定义字段,因此我将能够循环遍历它们并添加它们(如上面所示的添加列),但我无法在部署之前知道。自定义字段也随时可能发生变化。


听起来你需要一个 Map<String, String> 和一个连接表。 - chrylis -cautiouslyoptimistic-
那具体会是什么样子? - Curtis Snowden
1个回答

0

对于Hibernate 5,您应该通过RegistryService构建MetaData,添加属性,然后通过MetaDataBootstrap native metadata)构建SessionFactory。类似这样:

public SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sessionFactoryBuilder) {    
  StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
  registryBuilder.applySettings(sessionFactoryBuilder.getProperties());
  Metadata metaData = getMetadataSources().buildMetadata(registryBuilder.build());

  PersistentClass gridMapping = metaData.getEntityBinding(AgreementsGrid.class.getName());

  Column column = new Column();
  ...
  Property prop = new Property();
  ...
  gridMapping.addProperty(prop);

  SessionFactory sessionFactory = metaData.buildSessionFactory();
  return sessionFactory;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接