Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate SELECT causes UPDATE

This question is similar to the following:

What caused hibernate generate a update clause?

But it seems to not have answer

The log says the following is the update message. We are not explicitly doing this, however. Hibernate is somehow auto generating this upon a SELECT statement

update
        ops2.dbo.ObjectA
    set
        AcceptDate=?,
        ActionTaken=?,
        ModifyBy=?,
        ModifyByID=?,
        ModifyDate=?,
        ModifyDept=?,
        ParentId=?,
        Priority=?,
        RepRqmt=?,
        SchedDate=?,
        SchedDate=?,      
    where
        Rank=?

This is the statement that generates the problem:

  Query query =
            session.createSQLQuery("SELECT * FROM ProductOrders").addEntity(MyOrder.class);

        List<MyOrder> orders= query.list();
like image 841
75inchpianist Avatar asked Apr 02 '14 17:04

75inchpianist


1 Answers

By default, Hibernate flushes the pending changes before executing a query, to make sure that the query sees the changes that you've made before executing this query. If it didn't you could have this frustrating situation:

Foo foo = (Foo) session.get(Foo.class, 1L);
foo.setColor("red");
List<Foo> redFoos = 
    session.createQuery("select foo from Foo foo where foo.color = 'red'");
if (redFoos.isEmpty()) {
    System.out.println("WTF?");
}
like image 170
JB Nizet Avatar answered Oct 09 '22 22:10

JB Nizet