Announcement Announcement Module
Collapse
No announcement yet.
Configuring Hibernate event listeners Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuring Hibernate event listeners

    I am trying a small sample project which configures HibernateEventListeners. My xml file is:

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>		
    <beans xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    	http://www.springframework.org/schema/context 
    	http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    	http://www.springframework.org/schema/jdbc 
    	http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
    	http://www.springframework.org/schema/data/jpa 
    	http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd 
    	http://www.springframework.org/schema/tx 
    	http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" 
    	xmlns:tx="http://www.springframework.org/schema/tx" 
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    	xmlns:context="http://www.springframework.org/schema/context" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns="http://www.springframework.org/schema/beans">
    		
    	<!--  Note we added the jpa namespace to this configuration.-->
    
        <!-- Use the H2 database for testing -->
    	<jdbc:embedded-database id="dataSource" type="H2">
    		<jdbc:script location="classpath:schema.sql"/>
    		<jdbc:script location="classpath:test-data.sql"/>
    	</jdbc:embedded-database>		
    	
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="emf"/>
    	</bean>
    	
    	<tx:annotation-driven transaction-manager="transactionManager" />
    	
    	<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    		</property>
    		<property name="packagesToScan" 
    			value="com.apress.prospring3.ch10.domain"/>
    		<property name="jpaProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.H2Dialect
    				</prop>
    				<prop key="hibernate.max_fetch_depth">3</prop>
    				<prop key="hibernate.jdbc.fetch_size">50</prop>
    				<prop key="hibernate.jdbc.batch_size">10</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				
    				<!-- Listeners for Hibernate Envers -->
     				<prop key="hibernate.ejb.event.post-insert"> 
     					org.hibernate.ejb.event.EJB3PostInsertEventListener,
     					org.hibernate.envers.event.AuditEventListener 
     				</prop> 
     				<prop key="hibernate.ejb.event.post-update"> 
     					org.hibernate.ejb.event.EJB3PostUpdateEventListener,
     					org.hibernate.envers.event.AuditEventListener 
     				</prop> 
     				<prop key="hibernate.ejb.event.post-delete"> 
     					org.hibernate.ejb.event.EJB3PostDeleteEventListener,
     					org.hibernate.envers.event.AuditEventListener 
     				</prop> 
     				<prop key="hibernate.ejb.event.pre-collection-update"> 
     					org.hibernate.envers.event.AuditEventListener 
     				</prop> 
     				<prop key="hibernate.ejb.event.pre-collection-remove"> 
     					org.hibernate.envers.event.AuditEventListener 
     				</prop> 
     				<prop key="hibernate.ejb.event.post-collection-recreate"> 
     					org.hibernate.envers.event.AuditEventListener 
     				</prop> 
    
    				<!-- Properties for Hibernate Envers -->
     				<prop key="org.hibernate.envers.audit_table_suffix">_H</prop> 
     				<prop key="org.hibernate.envers.revision_field_name">AUDIT_REVISION</prop> 
     				<prop key="org.hibernate.envers.revision_type_field_name">ACTION_TYPE</prop> 
     				<prop key="org.hibernate.envers.audit_strategy">org.hibernate.envers.strategy.ValidityAuditStrategy</prop> 
     				<prop key="org.hibernate.envers.audit_strategy_validity_end_rev_field_name">AUDIT_REVISION_END</prop> 
     				<prop key="org.hibernate.envers.audit_strategy_validity_store_revend_timestamp">true</prop> 
     				<prop key="org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name">AUDIT_REVISION_END_TS</prop> 
     			
    			</props>
    		</property>
    	</bean>
    	
    	<context:annotation-config/>
    	 
    	<context:component-scan base-package="com.apress.prospring3.ch10.service.springjpa"/> 
    	
    	<jpa:repositories base-package="com.apress.prospring3.ch10.repository" 
    			transaction-manager-ref="transactionManager" 
    			entity-manager-factory-ref="emf"/>
    	
    	<!-- Define the audit listener in the configuration. The <jpa:auditing ..> tag is to enable the Spring Data
    		 JPA auditing feature, while the bean auditorAwareBean is the bean providing the user information -->
     	<jpa:auditing auditor-aware-ref="auditorAwareBean"/> 
     	<bean id="auditorAwareBean" class="com.apress.prospring3.ch10.springjpa.auditor.AuditorAwareBean"/>			      
     				      
    </beans>
    I am Configuring the EntityManagerFactory for Entity Versioning. When I run the test program I get the following errors:

    .......
    2012-12-28 16:21:43,356 INFO [org.hibernate.cfg.Environment] - <Hibernate 3.6.8.Final>
    2012-12-28 16:21:43,356 INFO [org.hibernate.cfg.Environment] - <hibernate.properties not found>
    2012-12-28 16:21:43,356 INFO [org.hibernate.cfg.Environment] - <Bytecode provider name : javassist>
    2012-12-28 16:21:43,356 INFO [org.hibernate.cfg.Environment] - <using JDK 1.4 java.sql.Timestamp handling>
    2012-12-28 16:21:43,387 INFO [org.hibernate.ejb.Version] - <Hibernate EntityManager 3.6.8.Final>
    2012-12-28 16:21:43,403 INFO [org.hibernate.ejb.Ejb3Configuration] - <Processing PersistenceUnitInfo [
    name: default
    ...]>
    2012-12-28 16:21:43,450 INFO [org.hibernate.cfg.Configuration] - <Mapping package com.apress.prospring3.ch10.springjpa>
    2012-12-28 16:21:43,544 INFO [org.springframework.beans.factory.support.DefaultL istableBeanFactory] - <Destroying singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@ffdadcd: defining beans [dataSource,transactionManager,org.springframework. aop.config.internalAutoProxyCreator,org.springfram ework.transaction.annotation.AnnotationTransaction AttributeSource#0,org.springframework.transaction. interceptor.TransactionInterceptor#0,org.springfra mework.transaction.config.internalTransactionAdvis or,emf,org.springframework.context.annotation.inte rnalConfigurationAnnotationProcessor,org.springfra mework.context.annotation.internalAutowiredAnnotat ionProcessor,org.springframework.context.annotatio n.internalRequiredAnnotationProcessor,org.springfr amework.context.annotation.internalCommonAnnotatio nProcessor,org.springframework.context.annotation. internalPersistenceAnnotationProcessor,contactAudi tService,springJpaContactService,contactAuditRepos itory,contactRepository,org.springframework.data.r epository.core.support.RepositoryInterfaceAwareBea nPostProcessor#0,org.springframework.dao.annotatio n.PersistenceExceptionTranslationPostProcessor#0,o rg.springframework.orm.jpa.support.PersistenceAnno tationBeanPostProcessor#0,org.springframework.cont ext.config.internalBeanConfigurerAspect,org.spring framework.data.jpa.domain.support.AuditingEntityLi stener,org.springframework.data.jpa.domain.support .AuditingBeanFactoryPostProcessor,auditorAwareBean ,org.springframework.context.annotation.Configurat ionClassPostProcessor$ImportAwareBeanPostProcessor #0]; root of factory hierarchy>
    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExc eptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'emf' defined in class path resource [spring-data-app-context.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to instantiate specified event (post-update) listener class:

    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplic ationContext.registerBeanPostProcessors(AbstractAp plicationContext.java:728)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:449)
    at com.apress.propring3.ch10.SpringJpaAuditSample.mai n(SpringJpaAuditSample.java:18)
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'emf' defined in class path resource [spring-data-app-context.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to instantiate specified event (post-update) listener class:





    Things seem to die at creating the second event listener, the "post-update". The first one is created fine and in fact if I move the "post-update" to the last position, it still dies on that one.

    Thanks in Advance
    Mike

  • #2
    Update

    I noticed a jar version mismatch. One of my hibernate jars, hibernate-validator was at version 4.1.0 and the rest of my hibernate jars were 3.6.8. So I updated everything to 4.1.0 as there is no hibernate-validator.3.6.8.Final.jar available. When I run, now I get the error:Caused by: java.lang.ClassNotFoundException: Could not load requested

    Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.envers.event.AuditEventListener
    at org.hibernate.service.classloading.internal.ClassL oaderServiceImpl$1.findClass(ClassLoaderServiceImp l.java:99)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
    at org.hibernate.service.classloading.internal.ClassL oaderServiceImpl.classForName(ClassLoaderServiceIm pl.java:138)
    ... 35 more

    Evidently AuditEventListener was moved?

    Comment


    • #3
      So it turns out that as of hibernate.4.0.0, AuditEvenListeners are automatically instantiated according to the @Audit annotations in your code. So I removed the AuditEventListeners from my configuration file and the error instantiating the AuditEventListeners went away. However I now have a new excpetion:

      Exception in thread "main" java.lang.AbstractMethodError: org.joda.time.contrib.hibernate.PersistentDateTime .nullSafeSet(Ljava/sql/PreparedStatement;Ljava/lang/Object;ILorg/hibernate/engine/spi/SessionImplementorV
      at org.hibernate.type.CustomType.nullSafeSet(CustomTy pe.java:155)
      at org.hibernate.persister.entity.AbstractEntityPersi ster.dehydrate(AbstractEntityPersister.java:2599)
      at org.hibernate.persister.entity.AbstractEntityPersi ster.dehydrate(AbstractEntityPersister.java:2576)
      at org.hibernate.persister.entity.AbstractEntityPersi ster$4.bindValues(AbstractEntityPersister.java:275 7)
      at org.hibernate.id.insert.AbstractReturningDelegate. performInsert(AbstractReturningDelegate.java:57)
      at org.hibernate.persister.entity.AbstractEntityPersi ster.insert(AbstractEntityPersister.java:2764)
      at org.hibernate.persister.entity.AbstractEntityPersi ster.insert(AbstractEntityPersister.java:3275)
      at org.hibernate.action.internal.EntityIdentityInsert Action.execute(EntityIdentityInsertAction.java:81)
      at org.hibernate.engine.spi.ActionQueue.execute(Actio nQueue.java:362)
      at org.hibernate.engine.spi.ActionQueue.addResolvedEn tityInsertAction(ActionQueue.java:203)
      at org.hibernate.engine.spi.ActionQueue.addInsertActi on(ActionQueue.java:183)
      at org.hibernate.engine.spi.ActionQueue.addAction(Act ionQueue.java:167)
      at org.hibernate.event.internal.AbstractSaveEventList ener.addInsertAction(AbstractSaveEventListener.jav a:320)
      at org.hibernate.event.internal.AbstractSaveEventList ener.performSaveOrReplicate(AbstractSaveEventListe ner.java:287)
      at org.hibernate.event.internal.AbstractSaveEventList ener.performSave(AbstractSaveEventListener.java:19 3)
      at org.hibernate.event.internal.AbstractSaveEventList ener.saveWithGeneratedId(AbstractSaveEventListener .java:126)
      at org.hibernate.ejb.event.EJB3PersistEventListener.s aveWithGeneratedId(EJB3PersistEventListener.java:7 8)
      at org.hibernate.event.internal.DefaultPersistEventLi stener.entityIsTransient(DefaultPersistEventListen er.java:208)
      at org.hibernate.event.internal.DefaultPersistEventLi stener.onPersist(DefaultPersistEventListener.java: 151)
      at org.hibernate.event.internal.DefaultPersistEventLi stener.onPersist(DefaultPersistEventListener.java: 78)
      at org.hibernate.internal.SessionImpl.firePersist(Ses sionImpl.java:757)
      at org.hibernate.internal.SessionImpl.persist(Session Impl.java:732)
      at org.hibernate.internal.SessionImpl.persist(Session Impl.java:736)
      at org.hibernate.ejb.AbstractEntityManagerImpl.persis t(AbstractEntityManagerImpl.java:854)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.springframework.orm.jpa.SharedEntityManagerCre ator$SharedEntityManagerInvocationHandler.invoke(S haredEntityManagerCreator.java:240)
      at $Proxy37.persist(Unknown Source)
      at org.springframework.data.jpa.repository.support.Si mpleJpaRepository.save(SimpleJpaRepository.java:32 8)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.springframework.data.repository.core.support.R epositoryFactorySupport$QueryExecutorMethodInterce ptor.executeMethodOn(RepositoryFactorySupport.java :368)
      at org.springframework.data.repository.core.support.R epositoryFactorySupport$QueryExecutorMethodInterce ptor.invoke(RepositoryFactorySupport.java:349)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
      at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:110)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
      at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:155)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
      at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:202)
      at $Proxy39.save(Unknown Source)
      at com.apress.prospring3.ch10.service.springjpa.Conta ctAuditServiceImpl.save(ContactAuditServiceImpl.ja va:50)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:318)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:183)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :150)
      at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:155)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
      at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:110)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
      at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:202)
      at $Proxy43.save(Unknown Source)
      at com.apress.propring3.ch10.SpringJpaAuditSample.mai n(SpringJpaAuditSample.java:35)

      I have not been able to find out any information on this exception. I am using joda-time.1.3.jar and joda-time-hibernate.2.0.jar. There is no joda-time.2.0.jar available and when I try using java-time-hibernate.1.3.jar the exception does not go away.

      Anyone have information on this exception?
      Thanks
      Mike
      Last edited by MikeT; Dec 29th, 2012, 03:15 PM. Reason: Update

      Comment


      • #4
        Please use [ code][/code ] tags when posting stack traces that way they remain more readable...

        There have been changes in the hibernate APIs and those aren't reflected in the old joda-time integration stuff. Basically that isn't going to work with this version of hibernate. So either downgrade or find a newer version of the joda-time hibernate integration which works with hibernate 4.x.

        Note: Regarding the listeners as of hibernate 4 a lot of listeners are automatically detected you shouldn't need any of those in your configuration.

        Comment


        • #5
          Configuring Hibernate Listeners

          Originally posted by Marten Deinum View Post
          Please use [ code][/code ] tags when posting stack traces that way they remain more readable...

          There have been changes in the hibernate APIs and those aren't reflected in the old joda-time integration stuff. Basically that isn't going to work with this version of hibernate. So either downgrade or find a newer version of the joda-time hibernate integration which works with hibernate 4.x.

          Note: Regarding the listeners as of r 4 a lot of listeners are automatically detected you shouldn't need any of those in your configuration.
          Yes, I discovered this. Evidently the Author has upadated the code on GitHub, I have to chekc it out

          Comment

          Working...
          X