使用JPA覆盖@MappedSuperclass中定义的@Id [英] Overriding @Id defined in a @MappedSuperclass with JPA
问题描述
我有一个AbstractEntity类,该类由应用程序中的所有实体扩展,并且基本上充当标识符提供程序.
I have a class AbstractEntity that is extended by all the entities in my application and basically acts as an Identifier provider.
@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,因为它们需要有一个自定义序列生成器.如何做到这一点?
I have a case now where I need to define a separate Id for couple of my Entity classes as these need to have a custom Sequence Generator. How can this be achieved?
@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;
}
}
推荐答案
您不能这样做.检查此 GitHub示例(如果需要).
You can't do it. Check this GitHub example if you want.
在基类中定义@Id
后,将无法在子类中覆盖它,这意味着最好将@Id
责任留给每个具体的类.
Once you define the @Id
in a base class you won't be able to override it in a sub-class, meaning it's better to leave the @Id
responsibility to each individual concrete class.
查看这篇文章以获取更多详细信息.
Check out this article for more details.
这篇关于使用JPA覆盖@MappedSuperclass中定义的@Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!