【发布时间】:2015-07-08 16:53:01
【问题描述】:
我有一个 AbstractEntity 类,它由我的应用程序中的所有实体扩展,基本上充当标识符提供者。
@MappedSuperclass
public class AbstractEntity implements DomainEntity {
private static final long serialVersionUID = 1L;
/** This object's id */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="creation_date", nullable = false, updatable=false)
private Date creationDate = new Date();
/**
* @return the id
*/
public long getId() {
return this.id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
}
我现在有一个案例,我需要为我的几个实体类定义一个单独的 Id,因为它们需要有一个自定义序列生成器。如何做到这一点?
@Entity
@Table(name = "sample_entity")
public class ChildEntity extends AbstractChangeableEntity {
@Column(name = "batch_priority")
private int priority;
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
}
【问题讨论】:
-
一种选择可能是覆盖需要自定义的子类中的生成器名称,但这意味着您对所有实体使用相同类型的生成器(序列生成器)。查看this post 了解更多信息。如果您/我们可以覆盖子类中的整个生成器,那将是理想的,但我认为它目前无法工作。
标签: java hibernate jpa hibernate-mapping mappedsuperclass