1 Reply Latest reply on Dec 12, 2012 8:37 AM by Adam Warski

    Auditing with embedded annotations

    Andrzej Cichon Newbie

      There is no word in documentation how to do that, please somebody tell me how to audit the embedded classes?

      I use following example: http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

       

      Co My code is:

       

      @Entity
      @Table(name="projects_project")
      @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(name="disc_type", discriminatorType=DiscriminatorType.STRING)
      @DiscriminatorValue("project")
      @Audited
      public class Project implements Serializable {
      
          private static final long serialVersionUID = 1L;
          @Id
          @GeneratedValue
          @Getter @Setter
          private int id;
          (...)
          @OneToMany(targetEntity=ProjectParameter.class, mappedBy="pk.project", cascade=CascadeType.ALL)
          @Getter @Setter
          private List<ProjectParameter> projectParameters = new ArrayList<ProjectParameter>();
      
      

       

      @Entity
      @Table(name="projects_rel_project_parameter")
      @Audited
      @AssociationOverrides({
          @AssociationOverride(name="pk.project", joinColumns=@JoinColumn(name="project_id")),
          @AssociationOverride(name="pk.parameter", joinColumns=@JoinColumn(name="parameter_id"))
      })
      public class ProjectParameter implements Serializable {
      
          private static final long serialVersionUID = 1L;
      
          @EmbeddedId
          @Getter @Setter
          private ProjectParameterPK pk = new ProjectParameterPK();
      
          @Getter @Setter
          private double value;
      
          public Project getProject(){
              return pk.getProject();
          }
      
          public void setProject(Project project){
              pk.setProject(project);
          }
      
          public Parameter getParameter(){
              return pk.getParameter();
          }
      
          public void setParameter(Parameter parameter){
              pk.setParameter(parameter);
      }
      

       

      public class ProjectParameterPK implements Serializable {
      
          private static final long serialVersionUID = 1L;
          @ManyToOne
          @Getter @Setter
          private Project project;
          @ManyToOne
          @Getter @Setter
          private Parameter parameter;
      
      }
      
      

       

      I got exception:

       

      Caused by: org.hibernate.MappingException: Unable to read the mapped by attribute for projectParameters in pl.proedims.project.model.ProjectParameter!

          at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.getMappedBy(CollectionMetadataGenerator.java:589)

          at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.addOneToManyAttached(CollectionMetadataGenerator.java:179)

          at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.addCollection(CollectionMetadataGenerator.java:161)

          at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addValueInSecondPass(AuditMetadataGenerator.java:223)

          at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addValue(AuditMetadataGenerator.java:245)

          at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addProperties(AuditMetadataGenerator.java:258)

          at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateSecondPass(AuditMetadataGenerator.java:519)

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

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

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

          at org.hibernate.envers.event.EnversIntegrator.integrate(EnversIntegrator.java:64)

          at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:301)

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

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

          at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)

          at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)

          at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)

          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)

          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)

          ... 36 more