Search code examples
hibernatespring-roo

Build reusable modified / created columns for Hibernate


Is it possible to re-use defined columns in Hibernate?

I have about 20 tables, each one requiring at least modified and created and some other fields which are the same for all these columns.

I've tried doing this:

public class ModifiedCreated {

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date created;

    @Version
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date modified;

    public ModifiedCreated() {
        created = new Date();
        modified = new Date();
    }

    @PrePersist
    protected void onPersist() {
        if (created == null) {
            created = new Date();
        }
        if (modified == null) {
            modified = new Date();
        }
    }

    @PreUpdate
    protected void onUpdate() {
        modified = new Date();
    }

    public Date getModified() {
        return modified;
    }

    public Date getCreated() {
        return created;
    }
}

And Route.java

@RooJavaBean
@RooToString
@RooEntity
public class Route extends ModifiedCreated {

    private String name;

    @ManyToMany(cascade = CascadeType.ALL)
    private Set<Note> notes = new HashSet<Note>();
}

Is it possible to re-use columns in Hibernate?


Solution

  • annotate your ModifiedCreated class with @MappedSuperclass annotation (That is annotation for hibernate. I don't know what spring-roo do with this however)