13 Replies Latest reply on Feb 27, 2012 4:52 PM by adamw

    How do I configure envers in Configuration class

    ijabz

      Trying to use Envers, trouble is the documentation shows you what to do if you are using hibernate.cfg.xml or persistence.xml but Im just using properties with the Configuration class, whilst this is analagous to hibernate.cfg.xml I cant work out how to setup the enver listeners

       

      I have two tests, one to create the database which does not create any _AUD tables currently even though the tables have @Audited annotation added, and my second test that trys to get a revision unsuprisingly fails with

       

      org.hibernate.exception.SQLGrammarException: Table/View 'COVERIMAGE_AUD' does not exist.

       

      This is my config class

       

      public static Configuration getInitializedConfiguration()

          {

              Configuration config = new Configuration();

              config.setProperty("hibernate.connection.driver_class","org.apache.derby.jdbc.EmbeddedDriver");

              config.setProperty("hibernate.connection.url","jdbc:derby:C:/User/MESH/Database/songlayer");

              config.setProperty("hibernate.dialect","org.hibernate.dialect.DerbyDialect");

        //config.setProperty("org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator","org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider");

              config.setProperty("hibernate.connection.username","jaikoz");

              config.setProperty("hibernate.c3p0.min_size","20");

              config.setProperty("hibernate.c3p0.max_size","100");

              config.setProperty("hibernate.c3p0.timeout","300");

              config.setProperty("hibernate.c3p0.max_statements","50");

              config.setProperty("hibernate.c3p0.idle_test_period","3000");

              config.setProperty("hibernate.show_sql","true");

              config.setProperty("hibernate.ejb.event.post-insert","org.hibernate.envers.event.AuditEventListener");

              config.setProperty("hibernate.ejb.event.post-update","org.hibernate.envers.event.AuditEventListener");

              config.setProperty("hibernate.ejb.event.post-delete","org.hibernate.envers.event.AuditEventListener");

              config.setProperty("hibernate.ejb.event.pre-collection-update","org.hibernate.envers.event.AuditEventListener");

              config.setProperty("hibernate.ejb.event.pre-collection-remove","org.hibernate.envers.event.AuditEventListener");

              config.setProperty("hibernate.ejb.event.post-collection-recreate","org.hibernate.envers.event.AuditEventListener");

              //config.setProperty("hibernate.format_sql","true");

              config.addAnnotatedClass(SongParent.class);

              config.addAnnotatedClass(Song.class);

              config.addAnnotatedClass(CoverImage.class);

              config.addAnnotatedClass(CoverArt.class);

              return config;

          }

        • 1. Re: How do I configure envers in Configuration class
          adamw

          Which version of Envers? Though it looks fine.

           

          Take a look at Envers tests, they create a Configuratin object by hand as well:

          https://github.com/hibernate/hibernate-orm/blob/3.6/hibernate-envers/src/test/java/org/hibernate/envers/test/AbstractEntityTest.java

           

          Adam

          • 2. Re: How do I configure envers in Configuration class
            ijabz

            Hmm, this example uses EJb3Configuration but Im using org.hibernate.cfg.Configuration

             

            Its also for Hibernate 3.6 rather than 4.0

             

            I found the 4.0 one which introduces a whole load more concepts that Im not particular interested in

            https://github.com/hibernate/hibernate-orm/blob/4.0/hibernate-envers/src/matrix/java/org/hibernate/envers/test/AbstractEntityTest.java

             

            Back to the question , can I do all this through a Configuration class (even if its deprecated) or not.

            • 3. Re: How do I configure envers in Configuration class
              ijabz

              Found this class now

               

              https://github.com/hibernate/hibernate-orm/blob/4.0/hibernate-envers/src/matrix/java/org/hibernate/envers/test/AbstractSessionTest.java

               

              Which seems to be the thing I want except it doesnt seem to congigure any envers stuff except the Audit Strategy (whihc I htink is optional) so how does it work ?

              • 4. Re: How do I configure envers in Configuration class
                adamw

                Look in the 3.6 branch.

                 

                Adam

                • 5. Re: How do I configure envers in Configuration class
                  ijabz

                  Ive looked and I dont see a solution, can you be a bit more fortcoming please.

                  • 6. Re: How do I configure envers in Configuration class
                    ijabz

                    So relunctantly I've given up on trying to configure everything within code directly and instead created a hibernate.cfg.xml file containing

                     

                    <?xml version='1.0' encoding='utf-8'?>

                    <!DOCTYPE hibernate-configuration PUBLIC

                    "-//Hibernate/Hibernate Configuration DTD//EN"

                    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

                    <hibernate-configuration>

                    <session-factory>

                    <listener class="org.hibernate.envers.event.AuditEventListener" type="post-insert"/>

                    <listener class="org.hibernate.envers.event.AuditEventListener" type="post-update"/>

                    <listener class="org.hibernate.envers.event.AuditEventListener" type="post-delete"/>

                    <listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-update"/>

                    <listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-remove"/>

                    <listener class="org.hibernate.envers.event.AuditEventListener" type="post-collection-recreate"/>

                    </session-factory>

                    </hibernate-configuration>

                     

                    Then in my code is

                     

                    public static Configuration getInitializedConfiguration()

                    {

                            Configuration config = new Configuration();

                            URL url = Thread.currentThread().getContextClassLoader().getResource("hibernate.cfg.xml");

                     

                            try

                            {

                                File file = new File(url.toURI());

                                config.configure(file);

                            }

                            catch(Exception e)

                            {

                                e.printStackTrace();

                            }

                            config.setProperty("hibernate.connection.driver_class","org.apache.derby.jdbc.EmbeddedDriver");

                            config.setProperty("hibernate.connection.url","jdbc:derby:C:/User/MESH/Database/songlayer");

                            config.setProperty("hibernate.dialect","org.hibernate.dialect.DerbyDialect");

                            //config.setProperty("org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator","org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider");

                            config.setProperty("hibernate.connection.username","jaikoz");

                            config.setProperty("hibernate.c3p0.min_size","20");

                            config.setProperty("hibernate.c3p0.max_size","100");

                            config.setProperty("hibernate.c3p0.timeout","300");

                            config.setProperty("hibernate.c3p0.max_statements","50");

                            config.setProperty("hibernate.c3p0.idle_test_period","3000");

                            config.setProperty("hibernate.show_sql","true");

                            config.setProperty("org.hibernate.envers.audit_strategy", "org.hibernate.envers.strategy.ValidityAuditStrategy");

                            //config.setProperty("hibernate.format_sql","true");

                            config.addAnnotatedClass(SongParent.class);

                            config.addAnnotatedClass(Song.class);

                            config.addAnnotatedClass(CoverImage.class);

                            config.addAnnotatedClass(CoverArt.class);

                            return config;

                        }

                     

                     

                    yet still when I call

                     

                    new SchemaExport(config).create(true, true);

                     

                    it doesnt create the AUD tables

                    • 7. Re: How do I configure envers in Configuration class
                      ijabz

                      org.hibernate.envers.event.AuditEventListener  doesn'tt even seem to exist in Hibernate 4.0 so how you are meant to run this in Hibernate 4.0 is a mystery, Ive gone back to 3.6 but still not working, here is relevant part of my maven pom

                       

                      <dependency>

                            <groupId>org.hibernate</groupId>

                            <artifactId>hibernate-core</artifactId>

                            <version>3.6.0.Final</version>

                          </dependency>

                          <dependency>

                            <groupId>org.hibernate</groupId>

                            <artifactId>hibernate-envers</artifactId>

                            <version>3.6.0.Final</version>

                          </dependency>

                          <dependency>

                            <groupId>org.hibernate</groupId>

                            <artifactId>hibernate-c3p0</artifactId>

                            <version>3.6.0.Final</version>

                          </dependency>

                      • 8. Re: How do I configure envers in Configuration class
                        ijabz

                        Well it ocurred to me that the documentation glossed over creating the schema and didnt provide an example. Also there was nothing special I was doing when I called new SchemaExport(config).create(true, true) and I couldnt see how adding the event listeners or annotations would effect this.

                         

                        Eventually I found the problem you have to add

                         

                        config.setProperty(Environment.HBM2DDL_AUTO, "create-drop");

                         

                        to the configuration.

                         

                        But this doesnt seem to be documented anywhere !

                        • 9. Re: How do I configure envers in Configuration class
                          adamw

                          Ah, so you want to do a schema export. You didn't mention this before.

                           

                          So you can do it two ways:

                          * hbm2dll auto - set it to "update", and the schema will only be updated with changes, nothing will be dropped

                          * use the hibernate ant tool task: http://docs.jboss.org/hibernate/core/4.0/devguide/en-US/html/ch15.html#envers-generateschema

                           

                          Also, Envers configuration in 3.6 and 4 is different. 3.6 requires the listeners, 4 requires just the jar on the classpath. That's described in the docs

                           

                          Adam

                          • 10. Re: How do I configure envers in Configuration class
                            ijabz

                            Well I did state in the first post and in a number of mentions that no  _AUD tables were created, that was the problem.

                             

                            It didn't occur for me to read the section on ant because I dont use ant I use maven, dont you think you should document the hbm2dll option rather than expect people to use ant

                            • 11. Re: How do I configure envers in Configuration class
                              adamw

                              Well hbm2ddl is a Hibernate-Core feature, so it is documented in the core docs. The _AUD tables are created in the same way any other tables are.

                               

                              Adam

                              • 12. Re: How do I configure envers in Configuration class
                                ijabz

                                No thats the point, I create my tables from my classes using:

                                 

                                new SchemaExport(config).create(true, true);

                                 

                                which is the only way I knew to create tables from classes BUT that doesnt create the Audit Tables

                                • 13. Re: How do I configure envers in Configuration class
                                  adamw

                                  Ah! Just do AuditConfiguration.getFor(config) before runnig SchemaExport and you'll be good.

                                   

                                  Adam