-
1. Re: Programmatic Schema Creation - Audit Tables not Created.
fender963 Jun 9, 2010 2:02 PM (in response to 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 Jun 10, 2010 8:39 AM (in response to fender963)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 Oct 6, 2010 10:01 AM (in response to adamw)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() {@Overridepublic 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);
}
});