在运行时将字段动态添加到休眠实体

时间:2019-05-16 04:04:17

标签: java hibernate dynamic

我有一个带有自定义字段的应用程序-用户基本上可以通过选择字段的类型并为其指定名称来定义自定义字段。然后,将自定义字段显示为实体的一部分,并将提供给这些字段的数据保存到我的数据库中。在大多数情况下,我能够以编程方式并通过常规的休眠映射(即@OneToMany带注释的集合)处理这些问题而没有问题。但是,我目前面临一个问题。我们希望将这些自定义字段及其值用于“父”实体的实时报告。自定义字段值被映射为父实体内部的集合,但是出于报告目的,我需要将它们平放。我创建了一个视图,该视图提供了SQL方面我真正需要的东西-I followed this example to add dynamic pivoting and the resulting query is precisely how I'd like to display my information。当然,不是下面的图像,而是本质上是我的输出。

pivot1

pivot2

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

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

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

Manipulating metadata at runtime

//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;
    }
    */

 }

请清楚一点,我不能在编译时映射这些字段。它们纯粹是自定义的,完全由用户定义。在运行时,我将能够知道存在哪些自定义字段,因此我可以遍历它们并添加它们(就像我希望使用上面看到的add列那样),但是在部署之前我不知道。自定义字段也随时可能更改。

1 个答案:

答案 0 :(得分:0)

对于 Hibernate 5,您应该通过 RegistryService 构建 MetaData,添加属性,然后通过 MetaData (Bootstrap 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;
}