Envers + Inheritance
moejoe Jul 28, 2009 7:27 AMHi 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