75inchpianist
75inchpianist

Reputation: 4102

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

Upvotes: 5

Views: 2179

Answers (1)

JB Nizet
JB Nizet

Reputation: 691953

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?");
}

Upvotes: 6

Related Questions