简体   繁体   English

通用JPA基本实体分离的主键序列

[英]Common JPA base entity separate primary key sequences

I have a UserAuthenticationEntity extending from BaseEntity . 我有一个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;
  }
  ...
  ...

}

All future entities will extend BaseEntity class. 将来所有的实体都将扩展 BaseEntity类。

Is it possible that all entities inherit id property from BaseEntity class yet have separate sequences to generate primary keys? 是否所有实体都可以从BaseEntity类继承id属性,但又具有单独的序列来生成主键?

I know this is a wired scenario but I think having a base entity class is a great place to define id property for all entity classes but not being able to use different sequences is a kind of deal breaker. 我知道这是一个有线方案,但是我认为拥有一个基本实体类是为所有实体类定义id属性的好地方,但是不能使用不同的序列是一种破坏交易的方法。 Not sure it can be done. 不确定是否可以做到。

Any alternative/better design for entity classes is also welcome. 也欢迎对实体类进行任何替代/更好的设计。

Is a bad idea to use inheritance with entities id's, i think. 我认为,将继承与实体ID一起使用是一个坏主意。

Usually, each entity contains an id attrite with the @Id and @GeneratedValue annotarions and inheritance is used to inherit common attributes. 通常,每个实体都包含一个带有@Id和@GeneratedValue注释的id属性,并且继承用于继承公共属性。 But, if you want to try it, you can take a look to the question below: 但是,如果您想尝试一下,可以看一下下面的问题:

Overriding @Id defined in a @MappedSuperclass with JPA 使用JPA覆盖@MappedSuperclass中定义的@Id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 jpa 注释和存储库实现具有 2 个实体作为主键的实体 - How to implements entity with 2 entity as primary key with jpa annotation and repository 如果主键不为null,Spring数据JPA不允许持久化Entity - Spring data JPA does not allow an Entity to be peristed if the primary key is not null 更新Spring Boot实体管理器JPA的主键 - Updating primary key for spring boot entity manager jpa JPA,如何将Map中实体类型的值添加到主键? - JPA, How to add the value of entity type in Map to primary key? Spring Data JPA - 使用自定义ID /主键保存新实体 - Spring Data JPA - saving a new entity with custom id / primary key Vaadin, JPA, Spring - 具有组合主键的实体的简单形式 - Vaadin, JPA, Spring - simple form for entity with composed primary key 当第一个实体是具有指定主键的实体之后的后续实体时,如何使用 JPA 获取列表 - How to getList using JPA when the first entity is the following entity after the entity with specified primary key 如果我们使用 JPA 和 Hibernate 在源实体中有主键,则从目标实体中检索列值 - Retrieving column value from target entity if we have primary key in source entity using JPA and Hibernate Spring Data JPA:无法使用包含外键的复合主键保存实体 - Spring Data JPA: Cannot save entity with composite primary key which contains foreign key Spring data jpa 在空数据库中为具有包含外键的复合主键的实体创建错误的字段 - Spring data jpa creates wrong fields in empty database for entity with composite primary key that contains foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM