【问题标题】:How to get the id value from an EclipseLink ClassDescriptor?如何从 EclipseLink ClassDescriptor 获取 id 值?
【发布时间】:2023-01-07 04:08:04
【问题描述】:

我们目前有以下可用的软删除定制器:

public class SoftDeleteCustomizer implements DescriptorCustomizer {

  @Override
  public void customize(ClassDescriptor descriptor) {
    descriptor.getQueryManager().setDeleteSQLString(
        String.format("UPDATE %s SET record_status = 'D', record_status_time = CURRENT_TIMESTAMP WHERE id = #ID",
                      descriptor.getTableName()
        )
    );
  }

}

我们现在要添加删除记录的用户。我可以清理用户名,但我更愿意使用参数/参数。

我重写了定制​​程序并且没有为 #ID 设置参数,因为它已经被正确地注入到某个地方。然后我发现当你使用DeleteObjectQuery(带参数?)时它没有被注入。所以我似乎必须为#ID添加一个参数,但我不知道如何获取要从ClassDescriptor中删除的记录/实体的id/主键值。

这是我到目前为止所拥有的:

  @Override
  public void customize(final ClassDescriptor descriptor) {
    final DeleteObjectQuery query = new DeleteObjectQuery();
    query.addArgument("DELETED_BY", String.class);
    query.addArgument("ID", Long.class);
    query.addArgumentValue(SecurityUtils.getUsername());
    query.addArgumentValue(...); // How to get the ID of the record to delete?
    query.setSQLString(String.format(DELETE_SQL, descriptor.getTableName()));
    descriptor.getQueryManager().setDeleteQuery(query);
  }

【问题讨论】:

    标签: jpa eclipselink


    【解决方案1】:

    好的,作为解决方法,我使用了我们的审计侦听器,我们将其添加为 EntityListeners 之一。它实现了SessionCustomizer。在那里我能够做到:

      @Override
      public void postDelete(final DescriptorEvent event) {
        final Long id = ((AbstractEntity) event.getObject()).getId();
        // Create and execute update query to set the username
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 2013-07-17
      • 2020-05-19
      相关资源
      最近更新 更多