使用JPA覆盖@MappedSuperclass中定义的@Id

问题描述 投票:6回答:3

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

}
java hibernate jpa hibernate-mapping mappedsuperclass
3个回答
5
投票

你不能这样做。如果需要,请检查这个GitHub example

一旦在基类中定义@Id,您将无法在子类中覆盖它,这意味着最好将@Id责任留给每个具体的类。

查看this article了解更多详情。


3
投票

拆分你的基类。

定义所有常见字段,但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生成的实体之外,不需要更改现有代码。


1
投票

在某些情况下不可行的解决方案之一是在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;
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.