-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate Interceptor: getEntity Problem !!
PostPosted: Thu Oct 26, 2006 3:06 pm 
Newbie

Joined: Thu May 25, 2006 9:45 am
Posts: 7
I am working on an application, where I have a hibernate Interceptor to handle the cache object (if an object is available in the cache then return it to the hibernate else put it in to cache for later use).

The Hibernate interceptor:

The getEntity method returns the Fully initialized object if it is available in cache otherwise it returns null. When getEntity method returns null, onLoad will be called and object will be put in the cache for future use. onLoad of interceptor will not be called when getEntity returns the fully initialized object from cache.

I have an object Track (maps to an hbm file) and can have many track_states (TrackState is another entity and maps to many-to-one relationship in Track.hbm.xml mapping file.

I was using the lazy initializtion for "trackstate". I tried changing the "TrackState" lazy initialization to false, but I get the same "Unknown Entity" error with different stack trace.


Hibernate configuration file


Code:
<!-- Hibernate Session -->
   <bean id="tranxSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource" ref="tranxDataSource" />
      <property name="lobHandler" ref="oracleLobHandler" />
      <property name="entityInterceptor" ref="modDateInterceptor" />
      <property name="eventListeners">
            <map>
                <entry key="merge">
                    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
                </entry>
            </map>
        </property>
      <property name="mappingResources">
         <list>
            <value>hibconf/Track.hbm.xml</value>
         ...
                                                ...
   <value>hibconf/Color.hbm.xml</value>
         </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.TreeCacheProvider</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.use_sql_comments">false</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.jdbc.batch_size">64</prop>
            <prop key="hibernate.default_batch_fetch_size">64</prop>
            <prop key="hibernate.connection.release_mode">after_transaction</prop>
            <prop key="hibernate.transaction.flush_before_completion">true</prop>
            <prop key="hibernate.transaction.auto_close_session">true</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
            <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</prop>

            <prop key="hibernate.max_fetch_depth">4</prop>
         </props>
      </property>
   </bean>



The interceptor code:

Code:
   

public Object getEntity(String entityName,
                            Serializable id)
    {
   // Need to cache only the TrackState items.
        if ( entityName.equalsIgnoreCase("TrackState"))
        {
            Object obj = getCacheEntity(key);
            return obj;
        }
    }

     // This method will be called when getEntity returns null
     // after checking that item doesn't exist in cache.
     public boolean onLoad(Object entity, Serializable id,
                   Object[] state, String[] propertyNames,
                   Type[] types)
    {
        String key = id.toString();
        return isEntityInCache(entity, id, state, propertyNames,
                            types, key);
    }

    public boolean isEntityInCache(Object entity, Serializable id,
                                   Object[] state, String[] propertyNames,
                                   Type[] types, String key)
    {
        if ( entity instanceof TrackState )
        {
            try
            {
                for (int ix =0; ix < propertyNames.length; ix++)
                {
                        logger.warn(" ---- Setting **" + propertyNames[ix] + "**"
                                    +" to : -- " + state[ix] );
         // Setting all the properties for the TrackState object.
                        PropertyUtils.setProperty(entity, propertyNames[ix], state[ix]);
                }
               
                // Putting the entity in to cache for the corresponding key.
                switchTreeCache.put("switch-1", key, entity);
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }
            catch (NoSuchMethodException e)
            {
                e.printStackTrace();
            }
            catch (CacheException e)
            {
                e.printStackTrace();
            }
       // Returning true.
            return true;
        }
        else
        {
            return false;
        }
    }

    public Object getCacheEntity(String key)
    {
        Object obj = null;
        try
        {
            obj = switchTreeCache.get("switch-1", key);
            if (obj != null)
            {
      // Item exists in the cache, return the same
                return obj;
            }
        }
        catch (CacheException e)
        {
            e.printStackTrace();
        }

   // Item doesn't exist in the cache, return null.
        return obj;
    }



track.hbm.xml

Code:
<hibernate-mapping package="com.uss.domain">

   <!-- Track -->
   <joined-subclass name="Track" entity-name="Track" table="track_device" extends="SVGable">
      <key column="track_device_id" />

      <property name="name" type="string" column="track_name" />
      <property name="lastModified" type="timestamp" column="last_mod_date_ts" />
      <property name="created" type="timestamp" column="create_date_ts" />

      <many-to-one name="state" entity-name="TrackState" column="track_device_id" insert="false" update="false" lazy="false" fetch="select"/>
   </joined-subclass>

   <!-- TrackState -->
   <class name="TrackState" entity-name="TrackState" table="track_state" lazy="false">
      <id name="id" type="long" column="track_device_id">
         <generator class="foreign">
            <param name="property">state</param>
         </generator>
      </id>

      <property name="decoration" type="string" column="decoration" />
      <property name="requestedPath" type="long" column="requested_path_id" />
      <property name="lastModified" type="timestamp" column="last_mod_date_ts" />
      <property name="created" type="timestamp" column="create_date_ts" />

      <many-to-one name="foregroundColor" column="foreground_color_id" class="Color" lazy="false"/>

      <many-to-one name="backgroundColor" column="background_color_id" class="Color" lazy="false"/>
   </class>




The stack trace when laxy initialization is set to false:


org.hibernate.MappingException: Unknown entity: com.uss.domain.TrackState
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
at org.hibernate.event.def.DefaultLockEventListener.onLock(DefaultLockEventListener.java:50)
at org.hibernate.impl.SessionImpl.fireLock(SessionImpl.java:584)
at org.hibernate.impl.SessionImpl.lock(SessionImpl.java:576)
at org.hibernate.impl.SessionImpl.getEntityUsingInterceptor(SessionImpl.java:482)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromSessionCache(DefaultLoadEventListener.java:428)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:334)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:137)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:101)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.ManyToOneType.assemble(ManyToOneType.java:196)
at org.hibernate.type.TypeFactory.assemble(TypeFactory.java:404)
at org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:96)
at org.hibernate.cache.entry.CacheEntry.assemble(CacheEntry.java:82)
at org.hibernate.event.def.DefaultLoadEventListener.assembleCacheEntry(DefaultLoadEventListener.java:540)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromSecondLevelCache(DefaultLoadEventListener.java:494)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:356)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:137)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:193)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:101)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.ManyToOneType.assemble(ManyToOneType.java:196)
at org.hibernate.collection.PersistentSet.initializeFromCache(PersistentSet.java:124)
at org.hibernate.cache.entry.CollectionCacheEntry.assemble(CollectionCacheEntry.java:35)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.initializeCollectionFromCache(DefaultInitializeCollectionEventListener.java:130)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:48)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
at com.uss.domain.ControlPoint.getLatestModified(ControlPoint.java:472)
at com.uss.domain.ControlPointGroup.getLatestModified(ControlPointGroup.java:255)
at com.uss.domain.Screen.toSVG(Screen.java:540)
at com.uss.domain.Screen.toSVG(Screen.java:431)
at com.uss.service.TrafficControlServiceImpl.getTracklineOverviewSvg(TrafficControlServiceImpl.java:253)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy10.getTracklineOverviewSvg(Unknown Source)
at com.uss.toa.controller.TracklineSvgController.getSVG(TracklineSvgController.java:300)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:351)
at org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:305)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
at com.uss.web.SwitchDispatcherServlet.doDispatch(SwitchDispatcherServlet.java:142)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:346)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:272)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:165)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3153)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1973)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1880)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1310)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:179)




Database used:
Oracle 10g

Am I doing something wrong or expecting something, which hibernate can't handle?

Thanks,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.