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

标签 java hibernate dynamic

我有一个带有自定义字段的应用程序 - 用户基本上可以通过选择字段类型并为其命名来定义自定义字段。然后将自定义字段显示为实体的一部分,并将提供给这些字段的数据保存到我的数据库中。在大多数情况下,我已经能够以编程方式和通过正常的 hibernate 映射(即@OneToMany 注释集合)毫无问题地处理这些问题。但是,我目前遇到了一个问题。我们希望将这些自定义字段及其值用于“父”实体的实时报告。自定义字段值被映射为父实体内的集合,但出于报告目的,我需要它们是扁平的。我创建了一个 View ,从 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

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

问题是我现在不知道如何使用 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 中处理这个问题。我有一个映射到 View 的基本实体,并且在运行时我需要能够动态地向它添加“字段”,以便我的DAO 可以动态过滤信息并处理排序/分组。

这是我的 View 实体:

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

 }

需要说明的是,我无法在编译时映射这些字段。它们纯粹是自定义的,完全由用户定义。在运行时,我将能够知道哪些自定义字段确实存在,因此我将能够遍历它们并添加它们(正如我希望对上面看到的添加列所做的那样),但我无法在部署之前知道。自定义字段也可能随时更改。

最佳答案

对于 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;
}

关于java - 在运行时向 Hibernate 实体动态添加字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56160669/

相关文章:

java - DataAccessException 无法捕获

java - 监控elipselink缓存L2,L1

java - 了解跨多个域的 JSessionId

java - 如何获取可嵌入的更改?

text - ImageMagick最适合矩形内的文本?

sql - 在 plpgsql 中,如何使用可变数量的标识符创建和执行命令字符串?

Java通过匿名类强制第三方类上的接口(interface)

java - Spring 悲观锁定

java - jsf hibernate 标准 api

c - 确定 C 中动态分配内存的大小