views:

696

answers:

2

I'm trying to get a simple envers example to work. I have the Java code working (apparently), and I finally got the org.hibernate.tool.ant.EnversHibernateToolTask to work, but it only outputs SQL for the "regular" data, not the versioning data. I'm stumped for what to do next.

What might I be forgetting to include?

Here is the SQL it outputs: {versioning-ddl.sql}

create table Event (
    id bigint generated by default as identity (start with 1),
    date timestamp,
    title varchar(255),
    primary key (id)
);

create table Person (
    id bigint generated by default as identity (start with 1),
    age integer not null,
    firstname varchar(255),
    lastname varchar(255),
    primary key (id)
);

create table PersonEvent (
    eventID bigint not null,
    personID bigint not null,
    primary key (eventID, personID)
);

alter table PersonEvent
    add constraint FK489E5C25F6E47374
    foreign key (personID)
    references Event;

alter table PersonEvent
    add constraint FK489E5C25729F3820
    foreign key (eventID)
    references Person;

I don't see any history tables/fields. Here are the relevant entries in my hibernate.cfg.xml file:

<hibernate-configuration>
   <session-factory>
        ...
     <property name="hibernate.ejb.event.post-insert">org.hibernate.envers.event.AuditEventListener</property>
     <property name="hibernate.ejb.event.post-update">org.hibernate.envers.event.AuditEventListener</property>
     <property name="hibernate.ejb.event.post-delete">org.hibernate.envers.event.AuditEventListener</property>
     <property name="hibernate.ejb.event.pre-collection-update">org.hibernate.envers.event.AuditEventListener</property>
     <property name="hibernate.ejb.event.pre-collection-remove">org.hibernate.envers.event.AuditEventListener</property>
     <property name="hibernate.ejb.event.post-collection-recreate">org.hibernate.envers.event.AuditEventListener</property>
     <mapping class="com.example.test.hibernate.test1.Event"/>
     <mapping class="com.example.test.hibernate.test1.Person"/>
   </session-factory>
</hibernate-configuration>

and relevant portions of Java classes:

 @Entity
 @Audited
 public class Event {
    @Id
    @GeneratedValue
    private Long id;
    private String title;
    private Date date;
    ...
 }


 @Entity
 @Audited
 public class Person {  
   @Id
   @GeneratedValue
   private Long id;

   private int age;
   private String firstname;
   private String lastname;
   ...
 }
A: 

Jason,

I am having this issue too - I had Envers working with Hibernate as long as it was configured using the JPA standard /META-INF/persistence.xml.

When I switched over to hibernate.cfg.xml, Envers is no longer being configured. Envers does however support the Hibernate Session which is what I'm trying to take advantage of since it is more powerful than the default JPA specifications.

Also, in case you didn't know, Envers will generate the auditing tables for you too, all you need are the annotations.

I'm still looking for an answer in the meantime, but if you can live without the Hibernate Session, you can get Envers up and running quickly by using Hibernate as a JPA provider configured through /META-INF/persistence.xml.

Walter

If you need a `Session` instead of a `EntityManager` why don't you use `EntityManager.getDelegate()`?
Willi
A: 

Be sure to annotate your entity POJOs with "@Audited". That might explain why the DDL for the audit tables is not being created.

benvolioT