8 Replies Latest reply on Jan 13, 2009 2:17 AM by adamw

    Envers & Hibernate - Queries

    kaizersh

      Hi,

      I'm using the hibernate-envers-3.4.0-SNAPSHOT.jar with Hibernate 3.3.1

      Please help me out with the following queries -

      1. Is it compulsory to use the AnnotationConfiguration with Envers? I have defined all my mappings in xml files and was using Configuration but i started getting ClassCastException in AuditConfiguration.

      2. I tried running the Ant task to generate the schema for the audit tables but I just cant get it to work. The schema for my entities are generated but none of the Audit tables are. I have used the org.hibernate.tool.ant.EnversHibernateToolTask class. Is it related to the fact that I'm not using Annotations?

      3. I have a bidirectional association between a Client and a ClientAddress. When I load a Client, the reference of Client set in the ClientAddress is Client_$$_javassist_0 which I think is the proxy. Now when I update or delete the client, I get a NPE -

      Exception in thread "main" java.lang.NullPointerException
      at org.hibernate.envers.event.AuditEventListener.generateBidirectionalCollectionChangeWorkUnits(AuditEventListener.java:107)
      at org.hibernate.envers.event.AuditEventListener.onPostDelete(AuditEventListener.java:153)
      at org.hibernate.action.EntityDeleteAction.postDelete(EntityDeleteAction.java:150)
      at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:117)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)

      This is because the entry in the EntitiesConfigurations Map is of Client and not Client_$$_javassist_0. Again, I'm not sure if this is related to Annotations. The only time it worked was while saving a new record.


      Thanks.

      Regards
      Kaizer

        • 1. Re: Envers & Hibernate - Queries
          adamw

          Hello,

          1. Yes, because it contains some reflection tools (class introspection etc) that Envers uses to read the annotations

          2. Yes, most probably :) Note that you only have to change the configuration class you instantiate, you can still map all of your classes using xml files

          3. Hmm... the entity name should be client in both cases. Can you please post your mappings and classes, or even better, create a JIRA issue with the failing test case?

          --
          Adam

          • 2. Re: Envers & Hibernate - Queries
            kaizersh

            Hi,

            Thanks for the reply!

            I tried out a JPA based configuration(using Annotations, EntityManager, etc) and all the 3 points above worked fine. Does this mean that Envers works only with a JPA config and not with native Hibernate? Is there any way i can achieve this?

            Also, I didnt understand the use of @AuditJoinTable in a @OneToMany scenario. The documentation says,
            When a collection is mapped using these two annotations, Hibernate doesn't generate a join table. Envers, however, has to do this, so that when you read the revisions in which the related entity has changed, you don't get false results.

            Does this mean I have to have a join table for the revisions to work correctly? Could you please give an example why this would be the case? I have a lot of OneToMany relations and creating Join Tables for all of them would just lead to too many tables which I dont really need.

            Thanks

            Regards
            Kaizer



            • 3. Re: Envers & Hibernate - Queries
              kaizersh

              Hi,

              I just found out that its possible to load hbm files from the persistence.xml config so I dont have to change my config. But I was just curious, does this mean Envers will work only with JPA? Also, I wonder why I'm not encountering point no 3 below.

              Thanks.

              Regards
              Kaizer

              • 4. Re: Envers & Hibernate - Queries
                adamw

                Hello,

                so when do you encouter point 3? When you use AnnotationConfiguration, but not Ejb3Configuration?

                The @AuditJoinTable is needed only if you have one specific mapping: unidirectional @OneToMany+@JoinTable (so without a 'partner' @ManyToOne). A normal @OneToMany(mappedBy="")+@ManyToOne or @OneToMany+@JoinTable will work without any additional configuration.

                The reason is that in that specific case, Hibernate doesn't generate a table to store the relation, but adds a column to the other entity (even though the column is not mapped to the class). Implementing it the same way in Envers would cause incorrect revisioning information.

                --
                Adam

                • 5. Re: Envers & Hibernate - Queries
                  kaizersh

                  Hi,

                  Yes, I get that exception on using AnnotationConfiguration. Also, the schema wasnt getting generated using . It all worked only on using JPA.

                  Thanks.

                  • 6. Re: Envers & Hibernate - Queries
                    adamw

                    Weird :) I'll have to review and fix that.
                    Could you create a JIRA bug?

                    --
                    Thanks,
                    Adam

                    • 7. Re: Envers & Hibernate - Queries
                      kaizersh

                      Hi,

                      I have created two Jira issues -

                      HHH-3705 - NPE encountered on using AnnotationConfiguration
                      HHH-3706 - Audit Table Schema not generated on using AnnotationConfiguration

                      Thanks.

                      Regards
                      Kaizer

                      • 8. Re: Envers & Hibernate - Queries
                        adamw

                        Ok, thanks :)

                        Adam