Igor Konoplyanko
Igor Konoplyanko

Reputation: 9374

Hibernate: executes update on select call

I'm having trouble with hibernate, i'm getting constraint violation exception on call, where I just want to call a 'select'.

return getHibernateTemplate().execute(new HibernateCallback<List<HibernateObject>>() {
    @Override
    public List<HibernateObject> doInHibernate(Session session) {
        Criteria criteria = session
          .createCriteria(HibernateObject.class)
          .add(eq("myobject.id", id));

        return criteria.list();
    }
});

Also update call happens for not HibernateObject, just for related object. How can I find out why update is happening there without my explicit calls?

Upvotes: 2

Views: 3450

Answers (2)

Nayan Wadekar
Nayan Wadekar

Reputation: 11602

You can either user StatelessSession or can clear the session to make entities detached to the persistent context.

  • clear : Completely clear the session. Evict all loaded instances and cancel all pending saves, updates and deletions. Do not close open iterators or instances of ScrollableResults.

  • StatelessSession : A stateless session does not implement a first-level cache nor interact with any second-level cache, nor does it implement transactional write-behind or automatic dirty checking, nor do operations cascade to associated instances. Collections are ignored by a stateless session. Operations performed via a stateless session bypass Hibernate's event model and interceptors. Stateless sessions are vulnerable to data aliasing effects, due to the lack of a first-level cache.

    For certain kinds of transactions, a stateless session may perform slightly faster than a stateful session.

Upvotes: 1

gadget
gadget

Reputation: 1978

By default hibernate uses FlushMode.AUTO which means:

The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.

An entity with dirty state must be attached to your session and Hibernate persists it before executing the query.

Upvotes: 6

Related Questions