3 Replies Latest reply on Oct 6, 2010 10:01 AM by cbear

    Programmatic Schema Creation - Audit Tables not Created.

    fender963

      Hey All,

       

      I am using programmatic schema export with the following code:

       

      AnnotationConfiguration cfg = new AnnotationConfiguration();

      AnnotationConfiguration cfg = new AnnotationConfiguration();
      cfg.setProperty( "hibernate.dialect", "org.hibernate.dialect.OracleDialect" );
      cfg.setProperty("hibernate.ejb.event.post-insert","org.hibernate.envers.event.AuditEventListener,com.mincom.mincom.tracker.EntityCUDListener");
      cfg.setProperty("hibernate.ejb.event.post-update","org.hibernate.envers.event.AuditEventListener,com.mincom.mincom.tracker.EntityCUDListener");
      cfg.setProperty("hibernate.ejb.event.post-delete","org.hibernate.envers.event.AuditEventListener,com.mincom.mincom.tracker.EntityCUDListener");
      cfg.setProperty("hibernate.ejb.event.pre-collection-update","org.hibernate.envers.event.AuditEventListener");
      cfg.setProperty("hibernate.ejb.event.pre-collection-remove","org.hibernate.envers.event.AuditEventListener");
      cfg.setProperty("hibernate.ejb.event.post-collection-recreate","org.hibernate.envers.event.AuditEventListener");
      cfg.setProperty("org.jboss.envers.revisionTypeFieldName","VER_REV_TYPE");
      cfg.setProperty("org.jboss.envers.revisionFieldName","VER_REV");
      
      cfg.addAnnotatedClass(myClass);
      
      InitialContext initContext = new InitialContext();
      SessionFactory sessFact = (SessionFactory) initContext.lookup( "mySF" );
      
      Connection conn = sessFact.openSession().connection();
      
      SchemaExport schemaExport = new SchemaExport( cfg, conn );
      schemaExport.create(true, true);
      
      
      
      

       

      This works just fine, except for none of the audit tables are created.

       

      However if I set all of these properties in persistence.xml and set  hibernate.hbm2ddl.auto = create-drop, in other words let Hibernate take care of this the first time it creates SessionFactory, there are no problems.

       

      Any thoughts?

        • 1. Re: Programmatic Schema Creation - Audit Tables not Created.
          fender963

          Figured it out.

           

          Have to call cfg.buildSessionFactory() for the audit tables to get created as well.

           

          Hope this helps someone.

          • 2. Re: Programmatic Schema Creation - Audit Tables not Created.
            adamw

            Yes, or do:

             

            configuration.buildMappings();
            AuditConfiguration.getFor(configuration);

             

            just as in the ant task (AnnotationConfigurationTaskWithEnvers).

             

            Adam

            • 3. Re: Programmatic Schema Creation - Audit Tables not Created.
              cbear

              It appears that this has changed in 3.6.0.CR2, due to a change in the way listeners are added in Hibernate

               

              This code worked for me, using the H2 Database, in standalone mode.

               

              final Configuration cfg = new Configuration().setProperty(Environment.URL, "jdbc:h2:test;DB_CLOSE_DELAY=-1;MODE=Oracle")
                      .setProperty(Environment.DRIVER, "org.h2.Driver").setProperty(Environment.HBM2DDL_AUTO, "create")
                      .setProperty(Environment.DIALECT, "org.hibernate.dialect.H2Dialect")
                      .setProperty("org.hibernate.envers.audit_strategy", "org.hibernate.envers.strategy.ValidityAuditStrategy");
                  AuditEventListener auditEventListener = new AuditEventListener();
                  cfg.setListener("post-insert", auditEventListener);
                  cfg.setListener("post-update", auditEventListener);
                  cfg.setListener("post-delete", auditEventListener);
                  cfg.setListener("pre-collection-update", auditEventListener);
                  cfg.setListener("pre-collection-remove", auditEventListener);
                  cfg.setListener("post-collection-recreate", auditEventListener);
                  addAnnotatedClasses(cfg);
                  cfg.setNamingStrategy(createNamingStrategy());
                  SessionFactory sessFact = cfg.buildSessionFactory();
                  AuditConfiguration.getFor(cfg);
                  sessFact.openSession().doWork(new Work() {
                    @Override
                    public void execute(final Connection conn) throws SQLException {
                      SchemaExport schemaExport = new SchemaExport(cfg, conn);
                      schemaExport.create(true, true);
                    }
                  });

                   Configuration cfg = new Configuration()

                         .setProperty(Environment.URL, "jdbc:h2:test;DB_CLOSE_DELAY=-1;MODE=Oracle")

                         .setProperty(Environment.DRIVER, "org.h2.Driver").setProperty(Environment.HBM2DDL_AUTO, "create")

                         .setProperty(Environment.DIALECT, "org.hibernate.dialect.H2Dialect")

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

               

                  AuditEventListener auditEventListener = new AuditEventListener();

                  cfg.setListener("post-insert", auditEventListener);

                  cfg.setListener("post-update", auditEventListener);

                  cfg.setListener("post-delete", auditEventListener);

                  cfg.setListener("pre-collection-update", auditEventListener);

                  cfg.setListener("pre-collection-remove", auditEventListener);

                  cfg.setListener("post-collection-recreate", auditEventListener);

               

                  SessionFactory sessFact = cfg.buildSessionFactory();

                  AuditConfiguration.getFor(cfg);

               

                  sessFact.openSession().doWork(new Work() {

                    @Override

                    public void execute(final Connection conn) throws SQLException {

                      SchemaExport schemaExport = new SchemaExport(cfg, conn);

                      schemaExport.create(true, true);

                    }

                  });