你有一个Hibernate实体的公共基类吗? [英] Do you have a common base class for Hibernate entities?

查看:107
本文介绍了你有一个Hibernate实体的公共基类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否拥有Hibernate实体的公共基类,即具有id,版本和其他常用属性的MappedSuperclass?

例子:

  @MappedSuperclass() 
public class BaseEntity {

private Long id;
私人长版;
...

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


解决方案

这对我们很好。除了ID和创建日期,我们还有修改日期。我们还有一个实现了 Taggable 接口的中间 TaggedBaseEntity ,因为我们的某些Web应用程序的实体具有标签,如堆栈溢出问题。


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

解决方案

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.

这篇关于你有一个Hibernate实体的公共基类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆