Hibernate / Spring HibernateTemplate.findByCriteria (Datched Criteria dc) does sql update on view

I am trying to search for a given criteria. This view has several fields for several different objects in my application that the user might want to find.

When I enter the name of the object that I want to find, before calling, .findByCriteria()

I add the constraint for the name field to the sandboxed criterion. This results in .findByCriteria()

getting a list of results with the name I am looking for.

Also, when I look at my log, I see that hibernate is calling select

statment.

Now I have added another object to my view with multiple search fields. When I try to find the field associated with this new entity, I get an exception in my log.

When I go through my log with the exception, I see that hibernate is calling a select

statment with the statement update

right after select

(I am not trying to update the entry, just load it into the list).

So why is hibernate calling an update when I call .findByCriteria()

on my new object?

    
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert (SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert (JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch (AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions (ActionQueue.java:266)

SQL that gets executed:

Sleep mode:

    select
    * 
from
    ( select
        this_.SEARCH_ID as SEARCH1_35_0_,
        this_.ST_NM as ST24_35_0_ 
    from
        SEARCH_RESULT this_ 
    where
        this_.LOAN_TYPE=? ) 
where
    rownum <= ?

      

DEBUG 2012-03-21 11: 37: 19,332 142195 (http-8181-3: org.springframework.orm.hibernate3.HibernateTemplate):
      [org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary (HibernateAccessor.java:389)] 
      Eagerly flushing Hibernate session

DEBUG 2012-03-21 11: 37: 19.384 142247 (http-8181-3: org.hibernate.SQL):
      [org.hibernate.jdbc.util.SQLStatementLogger.logStatement (SQLStatementLogger.java:111)] 
update
    SEARCH_RESULT 
set
    ADDR_LINE1=?,
    ASSGND_REGION=?,
    BASE_DEAL_ID=?,
    ST_NM=? 
where
    SEARCH_ID=?

      

+3


source to share


1 answer


There may have been an update because Hibernate is configured to autostart before executing queries, so if the persistence context thinks it has dirty data, it will try to update it. Without seeing the code, I can't be sure, but I would assume that even if search_result is a view, your corresponding Java object is annotated with getters and the object has comparable setters. Hibernate makes no distinction between tables and views, and if you call a setter, Hibernate will assume it has data changes to update.



You can customize how you create your Java objects for views by adding the @Immutable annotation (or hibernate. @Entity (mutable = false) depending on the version you are using. This should be enough to tell Hibernate not to flash the change. You you can also annotate the fields directly and get rid of your setters so that users of the SearchResult object know they are read-only.

+2


source







All Articles