How to get history var by root_proc_inst_id field?

how to get history var by root_proc_inst_id field ?

I try to create “HistoricVariableInstanceQuery” ,but it’s have no root method

Hi,

As you have noticed, this is not possible via HistoricVariableInstanceQuery.
I see two options:

  1. You can use HistoricVariableInstanceQuery to get a superset of the variables (using other filter criteria) and filter them in code.
  2. You can use NativeHistoricVariableInstanceQuery (see java doc) with a custom SQL query to get the instances.

I hope this helps

thank you for your answer

I decide to use NativeHistoricVariableInstanceQuery

but how to coding as select * from where xxx in (#{ids})

like this

public CommonResult<?> getSingleVarByRootProcInstId(@RequestParam("rootProcInstId") String rootProcInstId
            ,@RequestParam("varNames") List<String> varNames) {

        NativeHistoricVariableInstanceQuery htQuery = hisSvr.createNativeHistoricVariableInstanceQuery();
        List<HistoricVariableInstance> result = htQuery.sql("SELECT * FROM " + managementService.getTableName(HistoricVariableInstanceEntity.class) + " T WHERE T.ROOT_PROC_INST_ID_ = #{rootProcInstId} AND T.NAME_ IN (#{varNames})")
                .parameter("rootProcInstId", rootProcInstId)
                .parameter("varNames", varNames)
                .list();

above result is empty

Can you check by querying the database directly?
I did the following query and had no issues at all:

historyService.createNativeHistoricVariableInstanceQuery()
        .sql("SELECT * FROM ACT_HI_VARINST WHERE ROOT_PROC_INST_ID_ = #{rootProcInstId}")
        .parameter("rootProcInstId", delegateExecution.getVariable("rootProcInstId"))
        .list();

I mean how to set parameter by in condition like “var_Name in (“A”,“B”,“C”)"

Have you tried converting the Collection of varNames into a String (i.e., comma separated values)?

No .but I do this…LOL

 List<HistoricVariableInstance> result = htQuery.sql("SELECT * FROM " + managementService.getTableName(HistoricVariableInstanceEntity.class) + " T WHERE T.ROOT_PROC_INST_ID_ = #{rootProcInstId} AND T.NAME_ IN ("+varNamesCondition+") ORDER BY CREATE_TIME_ ASC")
                .parameter("rootProcInstId", rootProcInstId)
                //.parameter("varNames", varNamesCondition.toString())
                .list();

Just be careful that this can lead to SQL injection if an attacker can control the content of varNamesCondition. In comparison, the Mybatis templating will make it a parameter of a prepared statement which is safer.