11 Replies Latest reply on Jun 29, 2011 12:15 PM by adamw

    Audit Tables not getting generated .

    guesswt

      I was under the assumption that audit  tables get automatically created . But when I run from my app , I get the exception audit table does noes not exist .

       

      Here is a sample

       

      User Class - , just plain fields which is marked as  @Audited

      Here is my Persistence.xml

       

       

      <property name="hibernate.ejb.event.post-insert"
                   value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
                <property name="hibernate.ejb.event.post-update"
                   value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
                <property name="hibernate.ejb.event.post-delete"
                   value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
                <property name="hibernate.ejb.event.pre-collection-update"
                   value="org.hibernate.envers.event.AuditEventListener" />
                <property name="hibernate.ejb.event.pre-collection-remove"
                   value="org.hibernate.envers.event.AuditEventListener" />
                <property name="hibernate.ejb.event.post-collection-recreate"
                   value="org.hibernate.envers.event.AuditEventListener" />
            <property name="org.hibernate.envers.audit_table_suffix"
                   value="_VER" />
            <property name="org.hibernate.envers.revision_field_name"
                   value="REV" />
            <property name="org.hibernate.envers.revision_type_field_name"
                   value="REVTYPE" />   
            <property name="org.hibernate.envers.default_schema"
                   value="MY_AUDIT" />    
            <property name="org.hibernate.envers.revision_on_collection_change"
                   value="false" /> 
      Now After my publishing my app and running the application , when i create a new user I get the following error
      1. Hibernate _sequence not exist
      Then i Create the sequence
      2. Next I get this error ,  no table REVINFO found
        then i create the table
      3. Now I get no table USER_H  found
      Can somebody guide me if there are 'n'  number of classes which needs to be audited should i create those tables manually . I use eclipse to build & package  my EAR . There is no ant script .  involved
        • 1. Re: Audit Tables not getting generated .
          adamw

          The tables will get created automatically if you use hbm2ddl auto or update.

           

          Adam

          • 2. Re: Audit Tables not getting generated .
            guesswt

            I  had set it to update but Audit Tables are not getting created , Of course my Entitiees  normal tables are getting created .

             

             

            <properties>
                  <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/>
                  <property name="hibernate.default_schema" value="SCH"/>
                  <property name="hibernate.hbm2ddl.auto" value="update"/>
                  <property name="hibernate.show_sql" value="true"/>
                  <property name="hibernate.ejb.event.post-insert"
                         value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
                      <property name="hibernate.ejb.event.post-update"
                         value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
                      <property name="hibernate.ejb.event.post-delete"
                         value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
                      <property name="hibernate.ejb.event.pre-collection-update"
                         value="org.hibernate.envers.event.AuditEventListener" />
                      <property name="hibernate.ejb.event.pre-collection-remove"
                         value="org.hibernate.envers.event.AuditEventListener" />
                      <property name="hibernate.ejb.event.post-collection-recreate"
                         value="org.hibernate.envers.event.AuditEventListener" />
                  <property name="org.hibernate.envers.audit_table_suffix"
                         value="_VER" />
                  <property name="org.hibernate.envers.revision_field_name"
                         value="REV" />
                  <property name="org.hibernate.envers.revision_type_field_name"
                         value="REVTYPE" />  
                  <property name="org.hibernate.envers.default_schema"
                         value="MY_AUDIT" />   
                  <property name="org.hibernate.envers.revision_on_collection_change"
                         value="false" />              
                </properties>
            • 3. Re: Audit Tables not getting generated .
              adamw

              This should work without any problems. Can you see if AuditEventListener.initialize is getting called? (with a debugger)

               

              Adam

              • 4. Re: Audit Tables not getting generated .
                mart.schneider

                I have the same issue. I'm using Spring and created a TestNG-integration test, in which everything works fine. When I try to integrate Envers in my application (using the same configuration), my Revision-Table gets generated, but no Audit-tables. I have set hbm2ddl.auto=update and the AuditEventListener.initialize-method gets called on startup. Any ideas where to look for an error?

                 

                I'm using Hibernate 3.5.1-FINAL.

                • 5. Re: Audit Tables not getting generated .
                  adamw

                  Hmm if the initialize is called this "should" work - maybe make double sure that you have only one configuration file. Apart from that, you can try debugging the code that gets called in the initialization method - to see what happens there.

                   

                  Adam

                  • 6. Re: Audit Tables not getting generated .
                    mart.schneider

                    Thanks for the reply. After some trial & error and some more debugging it seems to be an issue with Spring and the way we use Hibernate in our project. We have a persistence module, where we define all database-relevant Spring beans (Session Factory, Transaction Manager...) as well as Envers. If I create an entity in this module and annotate it with @Audited, everything works as expected. However, for entities in other modules, no audit tables are created (however all other tables are created by hbm2ddl). My issue is probably due to the design of our application and not necessarily a problem with Envers, but to find the reason it would help to understand how (and where) Envers finds what entities should be audited.

                    • 7. Re: Audit Tables not getting generated .
                      adamw

                      Hello,

                       

                      that's very weird ... the way Envers finds entities is simple: if you look at AuditEventListener, at the bottom there's an initialize method that accepts a Configuration. You can get a list of entities from that. Try to debug that method a little and I think you'll quickly see how it works

                       

                      Adam

                      • 8. Re: Audit Tables not getting generated .
                        matbroughty

                        Hello,

                         

                        I am having a similar issue, no audit tables are created and neither is the REVINFO table.

                         

                        Added a breakpoint to AuditEventListener.initialize and that is called fine.  The Configuration passed to the AuditEventListener.initialize seems to have all the entities, so that also looks good.

                         

                        The hbm2ddl is set as follows in the hibernate.cfg.xml file (Hibernate 3.6.5)

                         

                        <property name="hbm2ddl.auto">update</property>

                         

                        I am using Spring (2.5.6) and so have added the listeners to the spring file (snippet below).

                         

                        <bean id="sessionFactory" class="com.gemini.database.hibernate.util.LocalSessionFactoryBean"
                              p:namingStrategy-ref="namingStrategy"
                              p:dataSource-ref="dsGemini"
                                >
                            <property name="mappingLocations">
                                <list>
                                    <value>classpath*:com/gemini/database/domain/*.hbm.xml</value>
                                </list>
                            </property>
                            <property name="hibernateProperties">
                                <props>
                                    <prop key="hibernate.show_sql">false</prop>
                                </props>
                            </property>
                            <property name="eventListeners">
                                <map>
                                    <entry key="post-insert">
                                        <ref local="enversListener"/>
                                    </entry>
                                    <entry key="post-update">
                                        <ref local="enversListener"/>
                                    </entry>
                                    <entry key="post-delete">
                                        <ref local="enversListener"/>
                                    </entry>
                                    <entry key="pre-collection-update">
                                        <ref local="enversListener"/>
                                    </entry>
                                    <entry key="pre-collection-remove">
                                        <ref local="enversListener"/>
                                    </entry>
                                    <entry key="post-collection-recreate">
                                        <ref local="enversListener"/>
                                    </entry>
                                </map>
                            </property>
                        </bean>

                         

                        <bean id="enversListener" class="org.hibernate.envers.event.AuditEventListener"/>

                         

                        But, although the app starts ok, no Enver Audit tables get generated, and if I try and alter the one entity that I have @Audited (com.gemini.database.domain.Configuration) I get the no table REVINFO found error:

                         

                         

                        Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [org.hibernate.envers.DefaultRevisionEntity]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [org.hibernate.envers.DefaultRevisionEntity]

                         

                        Caused by: java.sql.SQLException: Invalid object name 'REVINFO'.

                         

                        I put debug logging on the envers pacakge (log4j.category.org.hibernate.envers=DEBUG) but all I get is:

                         

                        INFO  org.hibernate.cfg.HbmBinder          - Mapping class: com.gemini.database.domain.Configuration_AUD -> CONFIGURATION_AUD
                        INFO  org.hibernate.cfg.HbmBinder          - Mapping class: org.hibernate.envers.DefaultRevisionEntity -> REVINFO

                        DEBUG guration.metadata.AuditMetadataGenerator - Generating first-pass auditing mapping for entity com.hpdsoftware.gemini.database.domain.Configuration.

                        DEBUG guration.metadata.AuditMetadataGenerator - Generating second-pass auditing mapping for entity com.hpdsoftware.gemini.database.domain.Configuration.

                         

                        Any ideas?  I'm a bit lost as to where to look next!

                         

                        Cheers

                         

                        Mat

                        • 9. Re: Audit Tables not getting generated .
                          matbroughty

                          I solved my issue - I think I may have imbibed too much last night.

                           

                          Basically I needed to put the hbm2ddl.auto bit in my spring file, not the hibernate cfg file, i.e.

                           

                          <property name="hibernateProperties">

                              <props>

                                          <prop key="hibernate.show_sql">false</prop>

                                          <prop key="hibernate.hbm2ddl.auto">update</prop>                       

                                  </props>

                          </property>

                           

                          As an aside, is it ok to have hibernate.hbm2ddl.auto as update in a production system?

                          • 10. Re: Audit Tables not getting generated .
                            hernanbolido

                            Hi!

                             

                            You can use variables for these properties and set them from outside. I'm using spring properties holders to achieve this.

                             

                            Regards. Hernán.

                            • 11. Re: Audit Tables not getting generated .
                              adamw

                              Great to hear it works

                              As for the question if update is safe for production - personally I usually have it set to validate, and generate the SQL update scripts using the staging/test database. But if you have a good staging environment, so that you can test the update before pushing it to production, I don't see a reason not to use it

                               

                              Adam