开发者

Custom EntityNotFoundDelegate

I get a org.hibernate.ObjectNotFoundException when I want to delete an object which doesn't exist anymore via hibernate. I just want this exception to be ignored. I could catch the exception and ignore, this would be a solution maybe. But, since there is a hibernate support for ignoring this exception through org.hibernate.cfg.Configuration#entityNotFoundDelegate, I would like to use its advantage and control it using configuration. The question is then, how can I introduce my own/custom implementation of EntityNotFoundDelegate to the org.hibernate.cfg.Configuration? Does anybody have a sample code for me? Just an additional tip, I use Spring Framework as well in my project.

Here is the exception that I get:

Caused by: org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [de.mycompany.domain.ResultObject#810b1334-32d3-02b0-e044-769e0ab48e48]; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [de.mycompany.domain.ResultObject#810b1334-32d3-02b0-e044-769e0ab48e48]
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:865)
   at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:859)
   at de.mycompany.utils.dao.impl.PersistentDaoImpl.delete(PersistentDaoImpl.java:50)
   at de.mycompany.utils.service.ServiceImpl.delete(ServiceImpl.java:68)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.springframework.aop.support.开发者_如何转开发AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
   at de.mycompany.utils.service.ServiceInterceptor.invoke(ServiceInterceptor.java:43)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy3.delete(Unknown Source)
   ... 14 more
Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [de.mycompany.domain.ResultObject#810b1334-32d3-02b0-e044-769e0ab48e48]
   at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:409)
   at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:108)
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:97)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
   at org.hibernate.engine.StatefulPersistenceContext.unproxyAndReassociate(StatefulPersistenceContext.java:594)
   at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:90)
   at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
   at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:793)
   at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:778)
   at org.springframework.orm.hibernate3.HibernateTemplate$26.doInHibernate(HibernateTemplate.java:871)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
   ... 30 more

And my versions:

Hibernate: 3.3.1

Spring: 2.5.6

Thanks in advance!

Felix


If you use an explicit Configuration, you can call the setEntityNotFoundDelegate method with a custom EntityNotFoundDelegate implementation that overrides handleEntityNotFound to do nothing.

Using Spring, this is implemented by specifying a custom SessionFactory. For example, you can extend LocalSessionFactoryBean and override buildSessionFactory to call sfb.setEntityNotFoundDelegate before building the session factory.

public class LocalSessionFactoryBeanIgnoringErrors extends LocalSessionFactoryBean {

    @Override
    protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
        sfb.setEntityNotFoundDelegate(new EntityNotFoundDelegate() {

            @Override
            public void handleEntityNotFound(String entityName, Serializable id) {
                System.out.println("Entity " + entityName + " with id " + id + " not found");
            }
        });
        return sfb.buildSessionFactory();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜