2013-07-22 263 views
0

我們使用hibernate將存儲過程結果映射到java對象。 它使用Hibernate 4.1.x版的SQL Server 2008hibernate存儲過程查詢緩存

@XmlRootElement(name = "hotel") 
@Entity 
@NamedNativeQuery(name = "fetchHotel", 
query = "{ call usp_iconnect_dashboard_gethotels(:screenname) }", 
resultSetMapping = "hotel-data", 
hints = {@QueryHint(name = "org.hibernate.callable", value = "true") } 
) 
@SqlResultSetMapping(name = "hotel-data", 
entities = @EntityResult(entityClass = Hotel.class)) 
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY) 
public class Hotel { 

    /** 
    * Unique hotel code. 
    */ 
    @Id 
    @Column(name = "hotelcode") 
    private String code; 

    /** 
    * Hotel name. 
    */ 
    @Column(name = "hotel") 
    private String name; 

    /** 
    * Indicates if user belongs to hotel. 
    * Y - if it is default hotel. 
    * N - if it is not default hotel. 
    */ 
    @Column(name = "is_default") 
    private String isDefault; 

    /** 
    * @return the code 
    */ 
    public final String getCode() { 
     return code; 
    } 

    /** 
    * @param code the code to set 
    */ 
    public final void setCode(final String code) { 
     this.code = code; 
    } 

    /** 
    * @return the name 
    */ 
    public final String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public final void setName(final String name) { 
     this.name = name; 
    } 

    /** 
    * @return the isDefault 
    */ 
    public final String getIsDefault() { 
     return isDefault; 
    } 

    /** 
    * @param isDefault the isDefault to set 
    */ 
    public final void setIsDefault(final String isDefault) { 
     this.isDefault = isDefault; 
    } 

} 

它的做工精細抓取結果上執行。

但啓用二級緩存(ehcache)和查詢緩存時。

<property name="hibernateProperties"> 
    <props> 
    <prop key="hibernate.dialect">${dialect}</prop> 
      <prop key="hibernate.show_sql">${showSQL}</prop> 
      <prop key="format_sql">true</prop> 
      <prop key="hibernate.current_session_context_class">thread</prop> 
      <prop key="hibernate.connection.release_mode">on_close</prop> 
      <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> 
      <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
      <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 
      <prop key="hibernate.cache.use_second_level_cache">true</prop> 
      <prop key="hibernate.cache.use_query_cache">true</prop> 
     </props> 
    </property> 

其工作正常,添加查詢緩存。然而在幾次呼叫之後,它的錯誤如下。

DEBUG StandardQueryCache - Checking cached query results in region: DailyExpire 
DEBUG EhcacheGeneralDataRegion - key: sql: { call usp_iconnect_dashboard_gethotels(?) }; parameters: ; named parameters: {screenname=dhulipah}; transformer: [email protected] 
DEBUG StandardQueryCache - Checking query spaces are up-to-date: [Hotel] 
DEBUG EhcacheGeneralDataRegion - key: Hotel 
DEBUG EhcacheGeneralDataRegion - Element for key Hotel is null 
DEBUG StandardQueryCache - Returning cached query results 
Hibernate: select hotel0_.hotelcode as hotelcod1_1_0_, hotel0_.is_default as is2_1_0_, hotel0_.hotel as hotel3_1_0_ from Hotel hotel0_ where hotel0_.hotelcode=? 
WARN SqlExceptionHelper - SQL Error: 208, SQLState: S0002 
ERROR SqlExceptionHelper - Invalid object name 'Hotel'. 
INFO DefaultLoadEventListener - HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: Invalid object name 'Hotel'. 
Jul 22, 2013 3:25:39 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException 
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container 
org.hibernate.exception.SQLGrammarException: Invalid object name 'Hotel'. 

在上面的日誌日誌中,它試圖在一段時間後作爲SELECT查詢而不是存儲過程調用來獲取。

任何想法爲什麼會發生這樣的事情? 因此,查詢緩存不能用於在hibernate中緩存存儲過程。 是否有任何其他方式使用緩存存儲過程結果?

非常感謝您的幫助。

回答