Hibernate runs unwanted update statement

后端 未结 1 836
不知归路
不知归路 2021-01-07 09:54

I have native query to run :

String sqlSelect =
\"select r.id_roster as id, \" +
                    \"count(roster_cat.id_category), \" +
            \"             


        
1条回答
  •  迷失自我
    2021-01-07 10:34

    What you describe is precisely what Hibernate's FlushMode.AUTO implies.

    Any modifications in the Persistence Context (1LC) at the time a query is executed will be automatically flushed prior to executing the query, guaranteeing that the results returned by the database match that which was cached by in-memory modifications.

    If the query is going to return entities that you're seeing the update for, then you should likely re-evaluate your operations, making sure that the query fires prior to the update to avoid the flush operation, which can be quite expensive depending on the volume of entities in your Persistence Context.

    If you are absolutely sure that the changes you're seeing flushed won't be returned by the query in question, you can always force the query not to cause a flush by setting the flush mode manually:

    Query query = session.createQuery( ... );
    query.setFlushMode( FlushMode.COMMIT );
    List results = query.list();
    

    But only do this if you're sure that the query wouldn't then be reading uncommitted changes as this can cause lots of problems and lead to long debug sessions to understand why changes are being inadvertantly lost by your application.

    0 讨论(0)
提交回复
热议问题