Results 1 to 2 of 2

Thread: Cached Hibernate Object not Showing

  1. #1

    Default Cached Hibernate Object not Showing

    OK I have jtaTransactions set up for the middle tier. I am using Spring 1.2.8 and Hibernate 3. I will post my files and code below. I am using OpenSessionInView with a singleSession for lazy loading.

    The problem is I add an object but when I try to display them on the screen the new object doesn't show until the cache times out. This is kind of URGENT for us if anyone could help. I believe the problem is with the transaction manager not being configured properly.

    Code:
       <bean id="factory" name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="mappingResources">
                <list>
                    <value>edu/bju/shared/model/people/mapping/PersonWareHouse.hbm.xml</value>
                    <value>edu/bju/shared/model/people/mapping/PersonWareHouseLite.hbm.xml</value>
                    <value>edu/bju/shared/model/groups/mapping/Query.hbm.xml</value>
                    <value>edu/bju/shared/model/groups/mapping/AppRoleQuery.hbm.xml</value>
                    <value>edu/bju/shared/model/groups/mapping/AppRoleQueryAudit.hbm.xml</value>
                    <value>edu/bju/shared/model/groups/mapping/AppRoleQueryAuditType.hbm.xml</value>
                    <value>edu/bju/shared/model/security/mapping/App.hbm.xml</value>
                    <value>edu/bju/shared/model/security/mapping/AppRole.hbm.xml</value>
                    <value>edu/bju/shared/model/security/mapping/AppRights.hbm.xml</value>
                    
    ....                
                </list>
            </property>
              <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="format_sql">true</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                    <prop key="hibernate.cache.use_second_level_cache">true</prop>
                    <prop key="hibernate.cache.use_query_cache">true</prop>
                    <prop key="hibernate.transaction.flush_before_completion">true</prop>
                </props>
            </property>
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
        </bean>
    
      <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
      </bean>
    
      <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
          <ref bean="jtaTransactionManager" />
        </property>
        <property name="transactionAttributes">
          <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="load*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="add*">PROPAGATION_REQUIRED</prop>
            <prop key="find*">PROPAGATION_REQUIRED</prop>
          </props>
        </property>
      </bean>
      
      <bean id="txAffirmative" parent="txProxyTemplate" abstract="true">
        <property name="preInterceptors">
          <list>
            <ref bean="annotationSecurityInterceptorAffirmativeBased" />
            <ref bean="securityInterceptorAffirmativeBased" />
          </list>
        </property>
      </bean>
    
      <bean id="appDAOHibernateManaged" parent="txAffirmative">
        <property name="target">
          <bean class="edu.bju.shared.model.security.dao.AppDAOHibernate">
            <property name="sessionFactory">
              <ref bean="factory" />
            </property>
          </bean>
        </property>
      </bean>
    
      <bean id="appRightsDAOHibernateManaged" parent="txAffirmative">
        <property name="target">
          <bean class="edu.bju.shared.model.security.dao.AppRightsDAOHibernate">
            <property name="sessionFactory">
              <ref bean="factory" />
            </property>
          </bean>
        </property>
      </bean>
    My AppRoleDAOHibernate

    Code:
    public class AppRoleDAOHibernate extends AbstractHibernateSpringDAO implements AppRoleDAO {
    
      @SuppressWarnings("unchecked")
      public ArrayList<AppRole> findAll() {
        return new ArrayList<AppRole>(super.findAll(AppRole.class, "appid"));
      }
    
      @SuppressWarnings("unchecked")
      public ArrayList<AppRole> findAllByAppId(long id) {
        HibernateTemplate ht = getHibernateTemplate();
        return new ArrayList<AppRole>(ht.find("from AppRole where appid = " + id));
      }
    
      @SuppressWarnings("unchecked")
      public ArrayList<AppRole> loadAll() {
        return new ArrayList<AppRole>(getHibernateTemplate().loadAll(AppRole.class));  
      }
      
      public AppRole find(long id) {
        return (AppRole)super.find(AppRole.class, new Long(id));
      }
    
      public void save(AppRole appRole) {
        super.saveOrUpdate(appRole);
      }
      
      public void delete(AppRole appRole) {
      	super.delete(appRole);
      }
    }
    
    public class AbstractHibernateSpringDAO extends HibernateDaoSupport implements GenericDAO {
    
      static Logger log = Logger.getLogger(AbstractHibernateSpringDAO.class);
      
      //private AclPermissionDAO aclPermissionDAO;
      //private AclObjectIdentityDAO aclObjectIdentityDAO;
      
      public AbstractHibernateSpringDAO() { }
      
      public void saveOrUpdate(Object obj) {
        getHibernateTemplate().saveOrUpdate(obj);
      }
      
      public void save(Object obj) {
      	getHibernateTemplate().save(obj);
      }
      
      public void delete(Object obj) {
        getHibernateTemplate().delete(obj);
      }
      
      public Object load(Class clazz, Long id) {
        return getHibernateTemplate().load(clazz, id);
      }
      
      public Object find(Class clazz, Long id) {
      	// return (getHibernateTemplate().find("from " + clazz.getSimpleName()+ " where id = " + id)).get(0);
      	//we want to use get() or load() here because they retrieve from 2nd level cache
      	return getHibernateTemplate().get(clazz, id);	
      }
      
      public List findAll(Class clazz) {
        return getHibernateTemplate().find("from " + clazz.getName());
      }
      
      public List findAll(Class clazz, String sortAttribute) {
      	 return getHibernateTemplate().find("from " + clazz.getName() + " order by " + sortAttribute);
      }
      
      public List loadAll(Class clazz) {
      	return getHibernateTemplate().loadAll(clazz);
      }
      
      public void clearAllCache(){
    	  getSession().clear();
      }
      /* (non-Javadoc)
       * @see edu.bju.aem.util.GenericDAO#startOperation()
       */
      public void startOperation() {
        // TODO Auto-generated method stub
      }
    }
    OK and here are 2 of the Hibernate mapping files
    Code:
    <hibernate-mapping schema="AEM" package="edu.bju.shared.model.security">
      <class name="AppRole" table="APPROLE" schema="AEM">
        <cache usage="read-write" />
        <id name="id" type="long" column="id">
          <generator class="edu.bju.shared.util.IDGenerator">
            <param name="tableName">APPROLE</param>
            <param name="schema">AEM</param>
          </generator>
        </id>
        <property name="role" type="string" column="ROLE" length="125" not-null="true">
        </property>
        <many-to-one name="app" entity-name="edu.bju.shared.model.security.App" cascade="none"  column="APPID">
        </many-to-one>
        <property name="description" type="string" column="DESCRIPTION">
        </property>
        <set name="appRights" sort="natural" cascade="none" inverse="true">
          <cache usage="read-write" />
          <key>
            <column name="APPROLEID" not-null="true" />
          </key>
          <one-to-many class="edu.bju.shared.model.security.AppRights" not-found="ignore" />
        </set>
      </class>
    </hibernate-mapping>
    
    <hibernate-mapping schema="AEM" package="edu.bju.shared.model.security">
      <class name="App" table="APPS" schema="AEM">
        <cache usage="read-write" />
        <id name="id" type="long" column="ID">
          <generator class="edu.bju.shared.util.IDGenerator">
            <param name="tableName">APPS</param>
            <param name="schema">AEM</param>
          </generator>
        </id>
        <property name="appname" type="string" column="APPNAME" length="250" not-null="true">
        </property>
        <property name="description" type="string" column="DESCRIPTION" length="32700">
        </property>
        <property name="entityPackage" type="string" column="ENTITYPACKAGE" length="250">
        </property>
        <property name="isCurrent" type="character" column="IS_CURRENT" length="1">
        </property>
        <set name="approles" cascade="none" inverse="true" order-by="role">
          <cache usage="read-write" />
          <key not-null="true" column="APPID">
          </key>
          <one-to-many entity-name="edu.bju.shared.model.security.AppRole" not-found="ignore" />
        </set>
      </class>
    </hibernate-mapping>
    and teh ehcache.xml

    Code:
        <!-- ehcache requires a default cache -->
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="86400"
            overflowToDisk="true"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            />
    		...              
            
        <cache name="edu.bju.shared.model.security.App"
               maxElementsInMemory="3000"
               overflowToDisk="false"
               timeToLiveSeconds="86400"
               eternal="false" />
            
        <cache name="edu.bju.shared.model.security.AppRole"
               maxElementsInMemory="10000"
               overflowToDisk="false"
               timeToLiveSeconds="86400"
               eternal="false" />
            
        <cache name="edu.bju.shared.model.security.AppRights"
               maxElementsInMemory="15000"
               overflowToDisk="false"
               timeToLiveSeconds="86400"
               eternal="false" />
    </ehcache>
    Last edited by LORDs_diakonos; Jul 13th, 2006 at 05:17 AM. Reason: added ehcache file

  2. #2

    Default

    2 more things of interest

    1. When I edit it works fine. Both the edit and the create call saveOrUpdate() on teh hibernate template.

    2. My delete wasn't working because it was removed from cache but other objects were still looking for it. I believe this is because of lazy initialization and teh lack of cascading. I set not-found="ignore" which seems to have fixed the delete problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •