[英]Common JPA base entity separate primary key sequences
I have a UserAuthenticationEntity extending from BaseEntity . 我有一个UserAuthenticationEntity从BaseEntity扩展。
@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.