10

我有一个 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;
    }

}
4

3 回答 3

9

你不能这样做,正如这个 GitHub 示例所示

一旦@Id在基类中定义了,就不能在子类中覆盖它,这意味着最好将@Id责任留给每个单独的具体类。

于 2015-04-29T12:58:46.393 回答
5

拆分你的基类。

定义除 ID 之外的所有常见字段:

@MappedSuperclass
public  abstract class AbstractEntityNoId implements DomainEntity {
 private static final long serialVersionUID = 1L;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="creation_date", nullable = false, updatable=false)
    private Date creationDate = new Date();
}

使用默认 ID 生成器进行上述扩展:

@MappedSuperclass
public abstract class AbstractEntity extends AbstractEntityNoId {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    public Long getId(){
       return id;
    }
}

需要自定义 ID 生成的类扩展了前者,而其他类扩展了后者。

有了上述内容,除了需要生成自定义 ID 的实体外,无需更改现有代码。

于 2015-04-29T13:54:39.120 回答
2

在某些情况下不可行的解决方案之一是使用 get 方法上的注释而不是字段。它将为您提供更大的灵活性,特别是覆盖您想要的任何内容。

在代码中:

@MappedSuperclass
public class AbstractEntity implements DomainEntity {

    private static final long serialVersionUID = 1L;

    protected long id;

    private Date creationDate = new Date();

    /**
     * @return the id
     */
    /** This object's id */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return this.id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="creation_date", nullable = false, updatable=false)
    public Date getCreationDate () {
        return this.creationDate ;
    }
}

还有你的子类:

@Entity
@Table(name = "sample_entity")
public class ChildEntity extends AbstractChangeableEntity {


private int priority;

@Override
@Id
@GeneratedValue(strategy = GenerationType.ANOTHER_STRATEGY)
public long getId() {
    return this.id;
}

@Column(name = "batch_priority")
public int getPriority() {
        return priority;
    }
public void setPriority(int priority) {
        this.priority = priority;
    }

}

于 2018-08-06T17:16:05.717 回答