Spring Data JPA
  1. Spring Data JPA
  2. DATAJPA-116

AbstractAuditable should have an equivalent annotation similar to @Audited of Hibernate Envers

    Details

      Description

      When adding auditing capabilities my domain class is forced to extend from org.springframework.data.jpa.domain.AbstractAuditable

      Patient.java
      @Entity
      public class Patient extends AbstractAuditable<User, Long> {
      ...
      }
      

      However, with Hibernate Envers, I just need to add @Audited

      Patient.java
      @Audited
      @Entity
      public class Patient {
      ...
      }
      

      Wouldn't it be nicer if we have an equivalent annotation for AbstractAuditable? Maybe like:

      Patient.java
      @Auditable(User.class, Long.class)
      @Entity
      public class Patient {
      ...
      }
      

      Thanks

        Issue Links

          Activity

          Hide
          Mark Serrano added a comment -

          I understand I'm "not really" forced to extend from AbstractAuditable but I can create my own class but then again, it's like creating from scratch when AbstractAuditable is already viable. And besides this custom class that I will be making isn't annotation too.

          Show
          Mark Serrano added a comment - I understand I'm "not really" forced to extend from AbstractAuditable but I can create my own class but then again, it's like creating from scratch when AbstractAuditable is already viable. And besides this custom class that I will be making isn't annotation too.
          Hide
          Oliver Gierke added a comment -

          I've started to sketch out a spec for a feature like this in a local ticket already so I'll just add it to the discussion here:

          To use the auditing feature of Spring Data JPA entities have to implement the Auditable interface. It would be cool if we could introduce less obtrusive annotations to trigger the very same behaviour. Beyond the reduced dependency it also nicely allows being very selective about what properties shall be set for different classes. Using annotations we could also allow using references to the ID type of the entity being referenced instead of the entity itself.

          I propose the following annotations to be introduced:

          • @CreatedBy
          • @CreatedDate
          • @LastModifiedBy
          • @LastModifiedDate
          class MyEntity {
          
            @CreatedBy
            private User createdBy;
            @LastModifiedBy
            private Long lastModifiedBy;
            @CreatedDate
            private DateTime createdDate;
            @LastModifiedDate
            private DateTime lastModifiedDate;
            
            // …
          }
          

          For the dates we should support java.util.Date and org.joda.time.DateTime out of the box and could look into supporting java.sql.Date as well. We should also look into options of moving parts of that infrastructure into Spring Data Commons as the general entity meta model lookup and manipulation could be used to implement auditing for other stores as well.

          Show
          Oliver Gierke added a comment - I've started to sketch out a spec for a feature like this in a local ticket already so I'll just add it to the discussion here: To use the auditing feature of Spring Data JPA entities have to implement the Auditable interface. It would be cool if we could introduce less obtrusive annotations to trigger the very same behaviour. Beyond the reduced dependency it also nicely allows being very selective about what properties shall be set for different classes. Using annotations we could also allow using references to the ID type of the entity being referenced instead of the entity itself. I propose the following annotations to be introduced: @CreatedBy @CreatedDate @LastModifiedBy @LastModifiedDate class MyEntity { @CreatedBy private User createdBy; @LastModifiedBy private Long lastModifiedBy; @CreatedDate private DateTime createdDate; @LastModifiedDate private DateTime lastModifiedDate; // … } For the dates we should support java.util.Date and org.joda.time.DateTime out of the box and could look into supporting java.sql.Date as well. We should also look into options of moving parts of that infrastructure into Spring Data Commons as the general entity meta model lookup and manipulation could be used to implement auditing for other stores as well.
          Hide
          Eugen Paraschiv added a comment -

          Separating these responsibilities would be a very good thing, as the audit needs do not usually fit these exact four concerns.
          Thanks.

          Show
          Eugen Paraschiv added a comment - Separating these responsibilities would be a very good thing, as the audit needs do not usually fit these exact four concerns. Thanks.
          Hide
          Nicholas Padilla added a comment -

          This does make sense to implement. It will also make it easier to upgrade existing code to use the Audit framework. I'll look at this a bit more myself next week.

          Show
          Nicholas Padilla added a comment - This does make sense to implement. It will also make it easier to upgrade existing code to use the Audit framework. I'll look at this a bit more myself next week.
          Hide
          Ranie Jade Ramiso added a comment -

          I am experimenting on this, my implementation is based on what Oliver proposed. Have not test it yet though, will do so in the weekend. The current implementation checks fields annotated with the proposed Annotations, and insert the appropriate values via reflection. My implementation revolves around the AuditingEntityListener.

          Show
          Ranie Jade Ramiso added a comment - I am experimenting on this, my implementation is based on what Oliver proposed. Have not test it yet though, will do so in the weekend. The current implementation checks fields annotated with the proposed Annotations, and insert the appropriate values via reflection. My implementation revolves around the AuditingEntityListener .
          Hide
          Oliver Gierke added a comment -

          Good to hear, Ranie Jade Ramiso! It would be perfect if you could already try to split up the implementation into stuff that could make it into Spring Data Commons (annotations, generic support infrastructure) and the Spring Data JPA specific adapter (i.e. the AuditingEntityListener implementation).

          If you don't have the time to get into that, feel free to submit a pull request of whatever kind. I'll sort that stuff out then myself.

          Show
          Oliver Gierke added a comment - Good to hear, Ranie Jade Ramiso ! It would be perfect if you could already try to split up the implementation into stuff that could make it into Spring Data Commons (annotations, generic support infrastructure) and the Spring Data JPA specific adapter (i.e. the AuditingEntityListener implementation). If you don't have the time to get into that, feel free to submit a pull request of whatever kind. I'll sort that stuff out then myself.
          Hide
          Ranie Jade Ramiso added a comment - - edited

          Hi Oliver I did separate the implementation. Take a look at it for the data commons part at my fork. I can submit a pull request if you like.

          Show
          Ranie Jade Ramiso added a comment - - edited Hi Oliver I did separate the implementation. Take a look at it for the data commons part at my fork . I can submit a pull request if you like.

            People

            • Assignee:
              Oliver Gierke
              Reporter:
              Mark Serrano
            • Votes:
              5 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: