简体   繁体   中英

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?

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.

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.

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