4 Replies Latest reply on Jul 29, 2009 3:32 AM by moejoe

    Envers + Inheritance

      Hi there,

      I wanted to know if i am doing this correctly. I have an abstract class called
      Request which all my request extend from . here is the class

      package za.co.aforbes.fpc.db.model.system.task;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Table;
      
      import org.hibernate.annotations.Cascade;
      import org.hibernate.envers.Audited;
      
      import za.co.aforbes.fpc.db.model.BaseModel;
      
      @Entity
      @Table(name = "Request")
      @Inheritance(strategy = InheritanceType.JOINED)
      public abstract class Request extends BaseModel<Integer> {
      
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       @Column(name = "ID")
       @Audited
       private Integer primaryId;
      
       @ManyToOne
       @Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
       @JoinColumn(name = "Task", nullable = false)
       @Audited
       private Task task;
      
       @ManyToOne
       @JoinColumn(name = "Status")
       private RequestStatus status;
      
       @ManyToOne
       @JoinColumn(name = "Type")
       private RequestType type;
      
       /**
       * Constructs a new {@link Request} model object.
       */
       protected Request() {}
      
       /**
       * Constructs a new {@link Request} model object.
       */
       public Request(RequestType.Type type) {
       if(type == null) {
       throw new IllegalArgumentException("type cannot be null.");
       }
       this.type = new RequestType(type);
       }
      
       /**
       * Returns the boolean representation if this request is pending.
       *
       * @return true if pending
       */
       public final boolean isPending() {
       return getStatus() != null && getStatus().isStatus(RequestStatus.Status.Pending);
       }
      
       /**
       * Returns the type of this request.
       *
       * @return the request type
       */
       public final RequestType getType() {
       return type;
       }
      
       /**
       * Set the type for this request.
       *
       * @param type the new request type
       */
       public final void setType(RequestType type) {
       this.type = type;
       }
      
       @Override
       public final Integer getPrimaryId() {
       return primaryId;
       }
      
       @Override
       public final void setPrimaryId(Integer primaryId) {
       this.primaryId = primaryId;
       }
      
       public final RequestStatus getStatus() {
       return status;
       }
      
       public final void setStatus(RequestStatus status) {
       this.status = status;
       }
      
       public final Task getTask() {
       return task;
       }
      
       public final void setTask(Task task) {
       this.task = task;
       }
      }
      
      


      Then i have a class that extends this Abstract request. which looks like this.
      package za.co.aforbes.fpc.db.model.system.task;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Table;
      
      import org.hibernate.annotations.OnDelete;
      import org.hibernate.annotations.OnDeleteAction;
      import org.hibernate.envers.Audited;
      import org.hibernate.envers.NotAudited;
      
      import za.co.aforbes.fpc.db.model.client.Product;
      import za.co.aforbes.fpc.db.model.system.task.RequestType.Type;
      
      @Entity
      @Table(name = "RequestSupplierClientID")
      @OnDelete(action = OnDeleteAction.CASCADE)
      @Audited
      public final class SupplierClientIdRequest extends Request {
      
       @ManyToOne
       @JoinColumn(name = "ProductID")
       @NotAudited
       private Product product = null;
      
       @Column(name = "NewMemberNumber")
       @NotAudited
       private String newMemberNumber = null;
      
       /**
       * Constructs a new {@link SupplierClientIdRequest} model object.
       */
       public SupplierClientIdRequest() {
       super(Type.SupplierClientId);
       }
      
       public final Product getProduct() {
       return product;
       }
      
       public final void setProduct(Product product) {
       this.product = product;
       }
      
       public final String getNewMemberNumber() {
       return newMemberNumber;
       }
      
       public final void setNewMemberNumber(String newMemberNumber) {
       this.newMemberNumber = newMemberNumber;
       }
      }
      
      


      Is this the way to do inheritance or is there an annotation i am missing. because the fields that are in the subclass are fields that i don't want to audit.

      thanks,
      Muhammed Patel

        • 1. Re: Envers + Inheritance
          adamw

          Well ... does it work, or do you get any problems?

          Adam

          • 2. Re: Envers + Inheritance

            :)
            Yes it works fine i just wanted to know if it is correct because with this configuration envers requires the following table SupplierClientIdRequest when i only want a request table. so i just wanted to know if there was a better way to do it.
            Thanks again,
            Muhammed Patel

            • 3. Re: Envers + Inheritance
              adamw

              Ah ... hmm, I guess you'll need the second table too, is you ever want to audit instances of SupplierClientIdRequest, so that envers could know which subclass it is.

              Adam

              • 4. Re: Envers + Inheritance

                i was just tinkering around and i found a way to ony have one table. i could just used the @AuditTable(value="parent-table") annotation and link that to the Request table. since i am not auditing anything in the subclass i can just the superclass log table.

                Thanks for coming to my rescue again !!

                Muhammed Patel