为什么我会得到与 JPA 共享的 ID?
Why do I get shared Ids with JPA?
我有一个关于 JPA 和继承 (EclipseLink) 的问题:
对于学校项目,我创建了一个摘要 class:HumanEntity
以及实现它的不同子classes。
摘要class:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class HumanEntity implements IHuman{
@Id
@GeneratedValue
protected long id;
@Column(name = "FIRST_NAME")
protected String firstName;
@Column(name = "LAST_NAME")
protected String lastName;
protected LocalDate birthDate;
protected HumanEntity(){
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public LocalDate getBirthDate() {
return birthDate;
}
}
子class 示例:
@Entity
@Table(name="ACTOR")
public class ActorEntity extends HumanEntity{
protected ActorEntity(){}
protected ActorEntity(ActorBuilder actorBuilder){
this.firstName = actorBuilder.getFirstName();
this.lastName = actorBuilder.getLastName();
this.birthDate = actorBuilder.getBirthDate();
}
}
当我 运行 项目时:
-> ID 为每个子 class 共享然后我有
在 Actor Table 中:
1 -> ....
3 -> ....
导演中table:
2 -> ....
如何为每个子class创建不同的 ID,但在我的代码中使用受保护(共享)的 ID?
另一个小问题 EclipseLink 或 JPA 总是创建一个序列 table 但我不知道它有什么用?我可以删除它吗?
非常感谢!
所有 HumanEntity
类型或子类的对象都是 HumanEntity
的实例,因此 ID 需要一致,因此来自相同的 "group" ID。
您不能为 Actor
说 id 1 和 为 Director
说 id 1 因为它们都是 HumanEntity
,而您需要唯一标识一个HumanEntity
。即 Actor#1 也是 HumanEntity#1。所以你不能同时拥有 Director#1 因为那也是 HumanEntity#1!
如果你想要像那些 id 那么你需要改变你的继承结构。
首先,您使用的是哪个数据库平台会很有趣。正如您提到的一个学校项目,我假设它是 PostgreSQL 或 MySQL?!
ID 的值基于策略(参见 GenerationType) that is used along with the @GeneratedValue
annotation. If you don't specify any specific strategy for a @GeneratedValue
-field, the default strategy should be javax.persistence.GenerationType.AUTO
according to the JPA JavaDoc of GeneratedValue. However EclipseLink seems to use javax.persistence.GenerationType.TABLE
according to their documentation:
GenerationType:
The strategy of the GeneratedValue is defined by the GenerationType enumerated type. The strategy defines how the id value should be generated on the database. By default, EclipseLink chooses the TABLE strategy using a table named SEQUENCE, with SEQ_NAME and SEQ_COUNT columns, with allocationSize of 50 and pkColumnValue of SEQ_GEN. If SEQUENCE is used the default database sequence used is named SEQ_GEN_SEQUENCE with an allocationSize of 50.
这基本上意味着 EclipseLink 将使用非业务数据库 table SEQUENCE
来查找下一个值。此外,它将使用此 table 的 SEQ_GEN
行来生成所有默认生成值。也就是说,默认情况下,所有实体都将共享一个生成值的公共池。我现在手头没有 DMBS,但研究这个 table 可能会让您对使用的值有更多的了解。
如果你想使用不同的策略或另一个 table 来生成你应该看看 TableGenerator and SequenceGenerator 的 JavaDoc。这样你就可以为每个子类创建一个单独的序列。
我想上面已经回答了你关于序列 table 的问题(至少我希望如此)。所以我宁愿不删除它...
我有一个关于 JPA 和继承 (EclipseLink) 的问题:
对于学校项目,我创建了一个摘要 class:HumanEntity 以及实现它的不同子classes。
摘要class:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class HumanEntity implements IHuman{
@Id
@GeneratedValue
protected long id;
@Column(name = "FIRST_NAME")
protected String firstName;
@Column(name = "LAST_NAME")
protected String lastName;
protected LocalDate birthDate;
protected HumanEntity(){
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public LocalDate getBirthDate() {
return birthDate;
}
}
子class 示例:
@Entity
@Table(name="ACTOR")
public class ActorEntity extends HumanEntity{
protected ActorEntity(){}
protected ActorEntity(ActorBuilder actorBuilder){
this.firstName = actorBuilder.getFirstName();
this.lastName = actorBuilder.getLastName();
this.birthDate = actorBuilder.getBirthDate();
}
}
当我 运行 项目时: -> ID 为每个子 class 共享然后我有
在 Actor Table 中: 1 -> .... 3 -> ....
导演中table: 2 -> ....
如何为每个子class创建不同的 ID,但在我的代码中使用受保护(共享)的 ID?
另一个小问题 EclipseLink 或 JPA 总是创建一个序列 table 但我不知道它有什么用?我可以删除它吗?
非常感谢!
所有 HumanEntity
类型或子类的对象都是 HumanEntity
的实例,因此 ID 需要一致,因此来自相同的 "group" ID。
您不能为 Actor
说 id 1 和 为 Director
说 id 1 因为它们都是 HumanEntity
,而您需要唯一标识一个HumanEntity
。即 Actor#1 也是 HumanEntity#1。所以你不能同时拥有 Director#1 因为那也是 HumanEntity#1!
如果你想要像那些 id 那么你需要改变你的继承结构。
首先,您使用的是哪个数据库平台会很有趣。正如您提到的一个学校项目,我假设它是 PostgreSQL 或 MySQL?!
ID 的值基于策略(参见 GenerationType) that is used along with the @GeneratedValue
annotation. If you don't specify any specific strategy for a @GeneratedValue
-field, the default strategy should be javax.persistence.GenerationType.AUTO
according to the JPA JavaDoc of GeneratedValue. However EclipseLink seems to use javax.persistence.GenerationType.TABLE
according to their documentation:
GenerationType:
The strategy of the GeneratedValue is defined by the GenerationType enumerated type. The strategy defines how the id value should be generated on the database. By default, EclipseLink chooses the TABLE strategy using a table named SEQUENCE, with SEQ_NAME and SEQ_COUNT columns, with allocationSize of 50 and pkColumnValue of SEQ_GEN. If SEQUENCE is used the default database sequence used is named SEQ_GEN_SEQUENCE with an allocationSize of 50.
这基本上意味着 EclipseLink 将使用非业务数据库 table SEQUENCE
来查找下一个值。此外,它将使用此 table 的 SEQ_GEN
行来生成所有默认生成值。也就是说,默认情况下,所有实体都将共享一个生成值的公共池。我现在手头没有 DMBS,但研究这个 table 可能会让您对使用的值有更多的了解。
如果你想使用不同的策略或另一个 table 来生成你应该看看 TableGenerator and SequenceGenerator 的 JavaDoc。这样你就可以为每个子类创建一个单独的序列。
我想上面已经回答了你关于序列 table 的问题(至少我希望如此)。所以我宁愿不删除它...