6 Replies Latest reply on May 14, 2011 12:00 PM by Adam Warski

    Audit subclass that extends non-audited superclass

    izan Newbie

      Hi all,

       

      I've got a MappingException when I try to audit an entity (VersionedEntity) that extends an abstract entity (BaseEntity) that is not audited. the inheritance strategy is TABLE_PER_CLASS, and I've tried to set this strategy also in the subclass (I've read something about that in the forums) with same result.

       

      Is there a way to set auditing in a subclass that extends a superclass that is not audited? What am I missing? Here are the versions used:

       

      • Envers: 1.2.3-hibernate-3.3
      • Hibernate-core: 3.3.1 GA
      • Hibernate-annotations: 3.4.0 GA
      • JBoss: 5.1.0.GA

       

      and this is the exception:

       

      Caused by: org.hibernate.MappingException: Entity 'VersionedEntity' is audited, but its superclass: 'BaseEntity' is not.

          at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateInheritanceMappingData(AuditMetadataGenerator.java:327)

          at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateFirstPass(AuditMetadataGenerator.java:408)

          at org.hibernate.envers.configuration.EntitiesConfigurator.configure(EntitiesConfigurator.java:100)

          at org.hibernate.envers.configuration.AuditConfiguration.<init>(AuditConfiguration.java:86)

          at org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:99)

          at org.hibernate.envers.event.AuditEventListener.initialize(AuditEventListener.java:335)

          at org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198)

          at org.hibernate.event.EventListeners.processListeners(EventListeners.java:181)

          at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194)

       

      I've dome some searching and couldn't not find a way to do it, but maybe I didn't search very well. Any help greatly appreciated!
      Izan.

        • 1. Audit subclass that extends non-audited superclass
          Adam Warski Master

          So you don't want to audit any of the fields from the base entity? (Apart from the id, which must be audited)

          If so, there's a feature request to implement that, but it's not yet done.

           

          Adam

          • 2. Re: Audit subclass that extends non-audited superclass
            izan Newbie

            Here is a code example of the abstract entity:

             

             

            @javax.persistence.Entity

            @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

            public abstract class Entity<ID extends Serializable>

              implements Serializable

            {

             

              @Id

              @Column(nullable=false)

              @GeneratedValue(strategy=GenerationType.TABLE)

              private Long id;

             

              public Long getId()

              {

                return this.id;

              }

             

              public void setId(Long id) {

                this.id = id;

              }

             

              public abstract ID getBusinessKey();

            }

             

             

            And here is the code for a subclass:

             

            @javax.persistence.Entity

            @SuppressWarnings("serial")

            @Audited

            public class TestClass extends Entity<String>{

             

             

                @Column(nullable = false)

                private String name;

             

                @Column(nullable = false)

                private Integer value;

             

             

                public void setName(String name) {

                    this.name = name;

                }

             

                public String getName() {

                    return name;

                }

             

                public void setValue(Integer value) {

                    this.value = value;

                }

             

                public Integer getValue() {

                    return value;

                }

             

                ......

                 Some overrides here

                ...... 

            }

             

             

             

            This, when deployed, throws the following exception

             

            Caused by: org.hibernate.HibernateException: could not init listeners

                at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:205)

                at org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1338)

                at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)

                at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)

                at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)

                ... 57 more

            Caused by: org.hibernate.MappingException: Entity 'com.model.entity.TestClass' is audited, but its superclass: 'com.model.entity.Entity' is not.

                at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateInheritanceMappingData(AuditMetadataGenerator.java:327)

                at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateFirstPass(AuditMetadataGenerator.java:408)

                at org.hibernate.envers.configuration.EntitiesConfigurator.configure(EntitiesConfigurator.java:100)

                at org.hibernate.envers.configuration.AuditConfiguration.<init>(AuditConfiguration.java:86)

                at org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:99)

                at org.hibernate.envers.event.AuditEventListener.initialize(AuditEventListener.java:335)

                at org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198)

                at org.hibernate.event.EventListeners.processListeners(EventListeners.java:181)

                at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194)

             

             

            Is it possible to audit this child class in some way?

            • 3. Re: Audit subclass that extends non-audited superclass
              izan Newbie

              Oh sorry, I didn't reload the page so I missed your answer before posting :-( The thing is that I don't want every subclass that inherits from the abstract class to be audited, and also that I don't have access to the base class since it is inside a library included in the project. No idea when this feature would be implemented, right? Is there aniway in which I could contribute to the development of this feature? Well, and to the development of envers in general.

               

              Thanks you very much!

              • 4. Re: Audit subclass that extends non-audited superclass
                Adam Warski Master

                I think this is related to this issue:

                http://opensource.atlassian.com/projects/hibernate/browse/HHH-5917

                (see the comments)

                 

                Am I right?

                 

                Adam

                1 of 1 people found this helpful
                • 5. Re: Audit subclass that extends non-audited superclass
                  izan Newbie

                  Totally right! Is there a solution for this in sight? How can I contribute?

                  • 6. Re: Audit subclass that extends non-audited superclass
                    Adam Warski Master

                    There's also another user interested in fixing the problem (see the comments on the issue), so I suppose the first step would be to work together on some design of how to solve the problem - that is make things configurable enough so that the superclasses may be both audited and not. So maybe let's move the discussion to the issue and take it from there

                     

                    Adam