簡體   English   中英

通用JPA基本實體分離的主鍵序列

[英]Common JPA base entity separate primary key sequences

我有一個UserAuthenticationEntityBaseEntity擴展。

@MappedSuperclass
public class BaseEntity implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_seq_generator")
  @SequenceGenerator(name = "user_seq_generator", sequenceName = "user_seq", allocationSize = 1)
  private Long id;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }
}

@Entity
@Table(name = "user_authentication")
public class UserAuthenticationEntity extends BaseEntity {

  private String username;
  private String password;

  public UserAuthenticationEntity(String username, String password){
    this.username = username;
    this.password = password;
  }
  ...
  ...

}

將來所有的實體都將擴展 BaseEntity類。

是否所有實體都可以從BaseEntity類繼承id屬性,但又具有單獨的序列來生成主鍵?

我知道這是一個有線方案,但是我認為擁有一個基本實體類是為所有實體類定義id屬性的好地方,但是不能使用不同的序列是一種破壞交易的方法。 不確定是否可以做到。

也歡迎對實體類進行任何替代/更好的設計。

我認為,將繼承與實體ID一起使用是一個壞主意。

通常,每個實體都包含一個帶有@Id和@GeneratedValue注釋的id屬性,並且繼承用於繼承公共屬性。 但是,如果您想嘗試一下,可以看一下下面的問題:

使用JPA覆蓋@MappedSuperclass中定義的@Id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM