Caleb Whittington
Caleb Whittington

Reputation: 107

Conditional @AttributeOverride Hibernate annotation

I have two tables FoodAudit and IngredientAudit that mirror (plus some additional columns) the tables Food and Ingredient as part of a revisions system I am working on. Ideally, both of these tables would map to RevisionDetail<T> objects in my domain model, but that's proving a bit tricky. To accomplish this, I want to embed either a Food or Ingredient object as shown below.

@Entity
public class RevisionDetail<T> {        
    @Column(name="RevisionId")
    private int revisionId;

    @Column(name="Operation")
    private Operation operation;

    @Embedded
    private T entity;

    public RevisionDetail() {}
}

Rather than Id, though, on the audit tables the column is FoodId or IngredientId on the audit tables. I would think to use the @AttributeOverride annotation, but the column name changes depending on whether it is a Food or Ingredient object. How might I go about accomplishing this?

Upvotes: 0

Views: 221

Answers (1)

Peter Š&#225;ly
Peter Š&#225;ly

Reputation: 2933

Consider to use Envers.

If you want to implement it by our own I suggest to use inheritance with @MappedSuperClass annotation on base type that will be extended by all auditable subclasses.

@MappedSuperClass
publci class BaseEntity{
    @Id 
    Long id;
}

Than it is possible to map entity to revision:

@OneToOne
@JoinColumn(name="fk_id")
BaseEntity entity;

@AttributeOverride can override only attributes of @Column annotation. Column name.

@Embeded can by used for @Embedable class which is not entity and does not has own table. Fields of embedable will by part of owning entity/table

Upvotes: 0

Related Questions