0

我有一个查询,它更新(table1)并插入到(table2)中,并在最后返回一个已由查询更新的id列表。

当我从 sql 运行它时,它运行正常。但是当我从我的代码运行时,它会返回受影响的 id,但它不会更新 & 行。

是因为我这样做是因为:namedQuery.list()??

SQL查询:

step 1: create table variable
step 2: update table1
step 3: store "Output Into" table variable; insert table variable content into table2 
step 4: retrun affected rows (table variable column)

这是sql:

Declare @tempHistoryTable Table(
    AG_TASK_ID int,Fulfillment_Request_ID int,Task_Type_ID int,Task_Status varchar(25)
    ,CF_System_User_ID int,Modified_By_System_User_ID int,AG_Task_Date datetime,row_version int
    )

    Declare @infoNeeded int,
                    @reviewResult int,
                    @researcherClass varchar(20),
                    @infoNeededReview int;

    Update ag_task Set task_status = case when task_status = 'awaitingHitEntry' or task_status = 'Uploaded' 
    then task_status when (data_source = 'New_Jersey' or data_source = 'Illinois') and :action = 'Assign'
    then 'sentForProcessing' else 'New' end
    ,db_version = case when db_version is null then 0 else db_version + 1 end
    ,modified_by_system_user_id = :assignedBy
    ,system_user_id = :assigningTo
    ,bpm_version = db_version
    ,task_type_id = case when task_type_id = @infoNeeded and :action = 'Assign'
    then @reviewResult
    when task_type_id = @infoNeeded and @researcherClass != 'External'
    then @reviewResult
    when (task_type_id = @infoNeededReview or task_type_id = @reviewResult) and @researcherClass = 'External'
    then @infoNeeded
    else task_type_id end
    ,creation_date = case when (task_type_id = @infoNeededReview or task_type_id = @reviewResult) and @researcherClass = 'External'
    then getDate() else creation_date end
    ,task_assigned_date = case when :assigningTo is null then null else GETDATE() end

    Output inserted.ag_task_id,inserted.Fulfillment_Request_ID,inserted.Task_Type_ID
    ,inserted.Task_Status,inserted.system_user_id
    ,inserted.Modified_By_System_User_ID,getdate(),inserted.row_version

    Into @tempHistoryTable
    Where fulfillment_request_id in (:fulfillmentRequestIds) and completion_date is null and 
    1 = case when data_source in ('New_Jersey', 'Illinois') and processing_date is null then 2 else 1 end
    and 1 = case when :action = 'Claim' and system_user_id is not null then 2 else 1 end
    and 1 = case when system_user_id is null and :assigningTo is null then 2 else 1 end

    insert into AG_TASK_HISTORY (
        AG_TASK_ID
        ,Fulfillment_Request_ID
        ,Task_Type_ID
        ,Task_Status
        ,CF_System_User_ID
        ,Modified_By_System_User_ID
        ,AG_Task_Date
        ,row_version
    ) 
    SELECT AG_TASK_ID
        ,Fulfillment_Request_ID
        ,Task_Type_ID
        ,Task_Status
        ,CF_System_User_ID
        ,Modified_By_System_User_ID
        ,AG_Task_Date
        ,row_version 
        from @tempHistoryTable

    select Fulfillment_Request_ID from @tempHistoryTable

这就是我调用代码的方式:

String queryName = AgTask.class.getName () + ".claimAssignTasks";
    final Query namedQuery = getNamedQuery ( queryName );
    logger.debug("assigningTo: "+assigningTo);
    logger.debug("assignedBy: "+assignedBy);
    logger.debug("action: "+action);
    logger.debug("fulfillmentRequestIdList: "+fulfillmentRequestIdList);
    namedQuery.setParameterList("fulfillmentRequestIds", fulfillmentRequestIdList);
    namedQuery.setParameter("assigningTo", assigningTo);
    namedQuery.setParameter("assignedBy",assignedBy);
    namedQuery.setParameter("action",action);
    logger.debug("Query: "+namedQuery.getQueryString());
    List<Integer> frIdList = new ArrayList<Integer>();
    frIdList = namedQuery.list();

这就是我运行测试的方式(它正确更新并插入到我的数据库中的表中):

@Test
public void testclaimAssignTasks(){
    transactionTemplate.execute(new TransactionCallbackWithoutResult ()
    {
        public void doInTransactionWithoutResult (TransactionStatus arg0)
        {
            Set<Integer> frIds = new HashSet<Integer>();
            frIds.add(190195);
            frIds.add(190257);
            frIds.add(190243);
            frIds.add(190205);
            //java.util.List<Integer> frIdList = 
            java.util.List<Integer> frIdList = agTaskSearchDao.claimAssignTasks(frIds,846,846,"Reassign");
            log.info("collection size: "+frIdList.size());
            for(Integer fulfillmentRequestId : frIdList){
                log.debug("fulfillmentRequestId: "+fulfillmentRequestId);
            }
        }

    });
}
4

1 回答 1

0

首先,我认为将如此复杂的查询放到 Hibernate/JPA 的上下文中是一个非常糟糕的主意。如果我是你,我会尝试分析 sql 并将其分解,以查看这是否可以通过借用 JPA/Hibernate 功能更清晰地解决。或者也许你最终会报废整个事情,用不同的方法解决手头的问题。

话虽如此,如果您要执行一个应该更改数据库状态的 SQL(或 HQL/JPQL),您将需要使用executeUpdate()Query 接口的方法,而不是list()用于uniqueResult()检索仅数据。

于 2013-09-23T18:48:26.910 回答