简体   繁体   中英

Update and return list from hibernate (using sql query)

I have a query which updates (table1) and inserts into (table2) and returns a list of ids at the end which have been updated by the query.

When I run it from sql it runs properly. But when I run from my code it returns the affected ids but it does not update & the rows.

Is it because I am doing it as: namedQuery.list() ??

SQL Query:

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)

Here's the 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

and this is how i'm calling the code:

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();

This is how i am running my test (which properly updates and inserts into table in my DB):

@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);
            }
        }

    });
}

First off, I think putting such a complex query into the context of Hibernate/JPA is a really bad idea. If I were you I would try to analyze the sql and break it down, in order to see if this is something that could be solved much cleaner by lending from JPA/Hibernate functionality. Or maybe you'll end up scrapping the whole thing, solving the problem at hand with different methods.

That being said, if you want to execute an SQL (or HQL/JPQL) that is supposed to change the state of the database, you will need to use the method executeUpdate() of the Query interface, as opposed to list() or uniqueResult() that is used for retrieval of data only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM