9 Replies Latest reply on Jun 30, 2010 8:22 AM by Syed Mahdi

    Spring and Envers

    Muhammed Patel Newbie

      Hi there,
      Please forgive me if this post has existed before but i have been trauing the net for the past week trying to find an answer. :)

      I am using spring 2.5.6 , hibernate 3.3 and have been trying to integrate Hibernate Enver 1.2.0 into my app.

      It seems that i get the tables to be created and the application starts up fine but when i make an update , create or delete my table is not populated with any data .

      My application-context.xml is as follows.

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
       <!-- START - IFA DATASOURCE AND SESSION FACTORY-->
       <!-- Below are connection values to the database. These values are used in the hibernate.properties file and are populated at runtime-->
       <bean id="ifaDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName">
       <value>net.sourceforge.jtds.jdbc.Driver</value>
       </property>
       <property name="url">
       <value>jdbc:jtds:sqlserver://localhost:1433/ifa;prepareSQL=0;SendStringParametersAsUnicode=False;</value>
       </property>
       <property name="username">
       <value>ifa</value>
       </property>
       <property name="password">
       <value>ifa</value>
       </property>
       <property name="initialSize">
       <value>2</value>
       </property>
       <property name="maxActive">
       <value>5</value>
       </property>
       <property name="maxIdle">
       <value>2</value>
       </property>
       </bean>
       <bean id="ifaSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
       <property name="dataSource">
       <ref bean="ifaDataSource" />
       </property>
       <property name="configLocation">
       <value>WEB-INF/hibernate/hibernate-ifa.cfg.xml</value>
       </property>
       <property name="configurationClass">
       <value>org.hibernate.cfg.AnnotationConfiguration</value>
       </property>
       <property name="hibernateProperties">
       <props>
       <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
       <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
       <prop key="hibernate.show_sql">true</prop>
       <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
       <prop key="hibernate.format_sql">true</prop>
       </props>
       </property>
       <property name="eventListeners">
       <map>
       <entry key="post-insert">
       <list>
       <bean class="org.hibernate.envers.event.AuditEventListener" />
       <ref bean="ejbInsert"/>
       </list>
       </entry>
       <entry key="post-update">
       <list>
       <bean class="org.hibernate.envers.event.AuditEventListener" />
       <ref bean="ejbUpdate"/>
       </list>
       </entry>
       <entry key="post-delete">
       <list>
      
       <bean class="org.hibernate.envers.event.AuditEventListener" />
       <bean class="org.hibernate.ejb.event.EJB3PostDeleteEventListener" />
       </list>
       </entry>
       <entry key="pre-collection-update">
       <bean class="org.hibernate.envers.event.AuditEventListener" />
       </entry>
       <entry key="pre-collection-remove">
       <bean class="org.hibernate.envers.event.AuditEventListener" />
       </entry>
       <entry key="post-collection-recreate">
       <bean class="org.hibernate.envers.event.AuditEventListener" />
       </entry>
       </map>
       </property>
       </bean>
       <bean name="callBack" class="org.hibernate.ejb.event.EntityCallbackHandler" />
      
       <bean name="audit" class="org.hibernate.envers.event.AuditEventListener" />
       <bean name="ejbInsert" class="org.hibernate.ejb.event.EJB3PostInsertEventListener" >
       <property name="callbackHandler">
       <ref bean="callBack"/>
       </property>
       </bean>
       <bean name="ejbUpdate" class="org.hibernate.ejb.event.EJB3PostUpdateEventListener" >
       <property name="callbackHandler">
       <ref bean="callBack"/>
       </property>
       </bean>
      
       <!-- END - IFA DATASOURCE AND SESSION FACTORY-->
      
      
      </beans>


      Also i have added the @Audited annotation to my bean.

      Any help would be much appreciated as i cannot find any resources for this problem.

      Many Thanks,
      Muhammed Patel



        • 1. Re: Spring and Envers
          Adam Warski Master

          Hello,

          unfortunately I don't have any experience with Spring. Did you read the blog post that is linked in the FAQ? And are you sure that the syntax for specifying multiple event listeners is correct?

          Adam

          • 2. Re: Spring and Envers
            Muhammed Patel Newbie

            Hmm.... i have had a look at the blogs but have not found anything that can help. unless i have missed something i will have a look again now.

            I am pretty sure that the syntax is correct. It creates the tabes which means it is registering. I think :)

            If you ever do come accross anything please let me know. Envers looks like a great concept but i can't get it to work in our configuration for some reason. and i am sure it is something silly .

            Thank you for the quick reply. it is very much appreciated.

            • 3. Re: Spring and Envers
              Adam Warski Master

              Well then ... do you have transactions configured properly and does the transaction commit?

              Adam

              • 4. Re: Spring and Envers
                Muhammed Patel Newbie

                Hi Adam,

                I am on a tomcat server so i don't have transactions.
                This may be a very silly question but do you need transactions to be setup for envers to work?

                If that is the case then i have to find a way to transactions to work on tomcat

                Muhammed

                • 5. Re: Spring and Envers
                  Adam Warski Master

                  Yes, you need transactions for Envers to work. You also probably need transactions anyway for your app to work properly ;)

                  Tomcat isn't an obstacle here, you can use transacitons there normally.

                  Adam

                  • 6. Re: Spring and Envers
                    Muhammed Patel Newbie

                    Thank you so much for clearing that up for me. i will go ahead and read into the transaction part of it.

                    I need to do allitle bit of research into transactions and tomcat and then can continue.

                    again thank you for your assistance. :D
                    Muhammed

                    • 7. Re: Spring and Envers
                      Varun Mehta Newbie

                      Everything looks good, at these two more lines and mark your test case as @Transactional

                       <!-- Transaction Manager -->
                       <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                       <property name="sessionFactory" ref="sessionFactory" />
                       </bean>
                      
                       <tx:annotation-driven transaction-manager="txManager" />
                      


                      Also by default Spring reverts all the database transactions, check your setting for it, change the defaultRollback = false.

                      Use these annotations on your test class.
                      @TransactionConfiguration(transactionManager = "txManager", defaultRollback = false)
                      @Transactional
                      


                      • 8. Re: Spring and Envers
                        Syed Mahdi Apprentice

                        I know this is pretty old, but i am having the same issue:

                         

                        Im on spring 2.5 as well and facing the exact same issue:

                         

                        Audit tables are created as they should but no update or insert entries go into them.

                        my spring.config.xml is listed here:

                        http://codepad.org/JBWjuA7k

                         

                        I have also tried with the EJB3postInsterlienters and the EJB3 listeners added to the events, but upon insert or update these events are not catcehd by AuditEventlister.

                         

                        I have the two lines mentioned by Varun as well and i hoped they would help but they didnt.

                         

                        Did the OP solve this issue somehow or gave up on envers

                        • 9. Re: Spring and Envers
                          Syed Mahdi Apprentice

                          Solved!!!

                           

                          incase some one needs this info and is stuck where I was stuck as well.

                          Need @transactional annotation on all service methods for the audited entities.  (will need aspectweaver.jar as well for aspectJ)

                           

                          then in Spring-config,.xml add <tx:annotationdriven> tag and register the transaactionmanager to it. thats all is needed.

                           

                           

                          In case I have missed out something message me

                           

                          thanks