Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you have a common base class for Hibernate entities?

Do you have a common base class for Hibernate entities, i.e. a MappedSuperclass with id, version and other common properties? Are there any drawbacks?

Example:

@MappedSuperclass()
public class BaseEntity {

    private Long id;
    private Long version;
    ...

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {return id;}

    public void setId(Long id) {this.id = id;}

    @Version
    public Long getVersion() {return version;}
    ...

    // Common properties
    @Temporal(TemporalType.TIMESTAMP)
    public Date creationDate() {return creationDate;}
    ...
}

@Entity
public class Customer extends BaseEntity {
    private String customerName;
    ...
}
like image 662
cretzel Avatar asked Oct 01 '08 08:10

cretzel


People also ask

What are hibernate entities?

A managed entity is a representation of a database table row (although that row doesn't have to exist in the database yet). This is managed by the currently running Session, and every change made on it will be tracked and propagated to the database automatically.

Can an entity class extend another class?

Entity classes can extend non-entity classes, and non-entity classes can extend entity classes. Entity classes can be both abstract and concrete.

Can the entity class inherit from non-entity classes?

Entities may have non-entity superclasses, and these superclasses can be either abstract or concrete. The state of non-entity superclasses is nonpersistent, and any state inherited from the non-entity superclass by an entity class is nonpersistent.

What is MappedSuperclass in JPA?

Annotation Type MappedSuperclassDesignates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.


3 Answers

This works fine for us. As well as the ID and creation date, we also have a modified date. We also have an intermediate TaggedBaseEntity that implements a Taggable interface, because some of our web application's entities have tags, like questions on Stack Overflow.

like image 175
Peter Hilton Avatar answered Sep 25 '22 11:09

Peter Hilton


The one that I use is primarily to implement hashCode() and equals(). I also added a method to pretty print the entity. In response to DR above, most of this can be overridden, but in my implementation you are stuck with an ID of type Long.

public abstract class BaseEntity implements Serializable {

    public abstract Long getId();
    public abstract void setId(Long id);

    /**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        return result;
    }

    /**
     * @see java.lang.Object#equals(Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BaseEntity other = (BaseEntity) obj;
        if (getId() == null) {
            if (other.getId() != null)
                return false;
        } else if (!getId().equals(other.getId()))
            return false;
        return true;
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return new StringBuilder(getClass().getSimpleName()).append(":").append(getId()).toString();
    }

    /**
     * Prints complete information by calling all public getters on the entity.
     */
    public String print() {

        final String EQUALS = "=";
        final String DELIMITER = ", ";
        final String ENTITY_FORMAT = "(id={0})";

        StringBuffer sb = new StringBuffer("{");

        PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(this);
        PropertyDescriptor property = null;
        int i = 0;
        while ( i < properties.length) {

            property = properties[i];
            sb.append(property.getName());
            sb.append(EQUALS);

            try {
                Object value = PropertyUtils.getProperty(this, property.getName());
                if (value instanceof BaseEntity) {
                    BaseEntity entityValue = (BaseEntity) value;
                    String objectValueString = MessageFormat.format(ENTITY_FORMAT, entityValue.getId());
                    sb.append(objectValueString);
                } else {
                    sb.append(value);
                }
            } catch (IllegalAccessException e) {
                // do nothing
            } catch (InvocationTargetException e) {
                // do nothing
            } catch (NoSuchMethodException e) {
                // do nothing
            }

            i++;
            if (i < properties.length) {
                sb.append(DELIMITER);
            }
        }

        sb.append("}");

        return sb.toString();
    }
}
like image 40
Matt Sidesinger Avatar answered Sep 26 '22 11:09

Matt Sidesinger


I wouldn't hesitate to use a common base class, after all that's the point of O/R mapping.

I use common base classes, too, but only if the entities share at least some common properties. I won't use it, if the ID is the only common property. Until now I did not encounter any problems.

like image 37
Daniel Rikowski Avatar answered Sep 25 '22 11:09

Daniel Rikowski