您是否有休眠实体的通用基类?

2022-09-02 10:54:06

您是否有Hibernate实体的通用基类,即具有id,version和其他常见属性的MappedSuperclass?有什么缺点吗?

例:

@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;
    ...
}

答案 1

这对我们来说很好。除了ID和创建日期外,我们还有修改日期。我们还有一个中间的 TaggedBaseEntity,它实现了一个可标记的接口,因为我们的一些 Web 应用程序的实体都有标记,比如 Stack Overflow 上的问题。


答案 2

我使用的那个主要是实现hashCode()和equals()。我还添加了一个方法来漂亮地打印实体。作为对上述DR的回应,其中大部分可以被覆盖,但是在我的实现中,您被一个Long类型的ID困住了。

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();
    }
}

推荐