在普通JPA或JPA + Hibernate扩展中是否可以声明复合键,其中复合键的元素是序列?
这是我的复合课程:
@Embeddable
public class IntegrationEJBPk implements Serializable {
//...
@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
@JoinColumn(name = "APPLICATION")
public ApplicationEJB getApplication() {
return application;
}
@Column(name = "ENTITY", unique = false, nullable = false, insertable = true, updatable = true)
public String getEntity() {
return entity;
}
@GeneratedValue(strategy = GenerationType.AUTO, generator = "INTEGRATION_ID_GEN")
@SequenceGenerator(name = "INTEGRATION_ID_GEN", sequenceName = "OMP_INTEGRATION_CANONICAL_SEQ")
@Column(name = "CANONICAL_ID", unique = false, nullable = false, insertable = true, updatable = true)
public String getCanonicalId() {
return canonicalId;
}
@Column(name = "NATIVE_ID", unique = false, nullable = false, insertable = true, updatable = true)
public String getNativeId() {
return nativeId;
}
@Column(name = "NATIVE_KEY", unique = false, nullable = false, insertable = true, updatable = true)
public String getNativeKey() {
return nativeKey;
}
//...
}
我已经提供了application
,entity
,nativeId
和nativeKey
的值。我想构建一个如下所示的实体:
IntegrationEJB i1 = new IntegrationEJB();
i1.setIntegrationId(new IntegrationEJBPk());
i1.getIntegrationId().setApplication(app1);
i1.getIntegrationId().setEntity("Entity");
i1.getIntegrationId().setNativeId("Nid");
i1.getIntegrationId().setNativeKey("NK");
当我致电em.persist(i1
)时,我希望生成canonicalId
并插入集成。
这可能吗?如果是这样,那简单的方法是什么? (我不想使用应用程序提供的密钥或本机sql。)
答案 0 :(得分:3)
我认为普通的JPA无法做到这一点。
答案 1 :(得分:2)
未使用JPA 2规范指定将@GeneratedValue用于复合PK。
来自JPA规范:
11.1.17 GeneratedValue Annotation
GeneratedValue注释提供了规范 主键值的生成策略。该 GeneratedValue注释可以应用于主键属性或 实体的字段或映射的超类与Id一起使用 注释。[97]仅使用GeneratedValue注释 需要支持简单的主键。使用的 派生主键不支持GeneratedValue注释。
请注意,在不同的场景下,您可以让父实体使用@GeneratedValue的简单PK(@Id),然后让子实体具有包含父级生成的ID的复合PK,以及另一栏。
@Entity public class ParentEntity {
@Id
@GenerateValue(IDENTITY) // If using DB auto-increment or similar
int id;
// ...
}
@Entity @IdClass(ChildId.class) public class ChildEntity { // The child table will have a composite PK: // (parent_ID, child_name) @Id @ManyToOne int parentEntity;
@Id
String childName;
String favoriteColor; // plus other columns
// ...
}
// child Id attributes have the same name as the child entity // however the types change to match the underlying PK attributes // (change ParentEntity to int) public class ChildId implements Serializable {
int parentEntity;
String childName;
public ChildId() { //... }
// Add extra constructor & remove setter methods so Id objects are immutable
public ChildId(int parentEntity, String childName) { //... }
public int getParentEntity() { //... }
// make Id objects immutable:
// public void setParentEntity(int parentEntity) { //... }
public String getChildName() { //... }
// public void setChildName(String childName) { //... }
}
答案 2 :(得分:1)
试试这样:
@TableGenerator(name = "canonicalKeys", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "canonicalKeys")
@Column(name = "CANONICAL_ID", unique = false, nullable = false, insertable = true, updatable = true)
public String getCanonicalId() {
return canonicalId;
}
通过这种方式,您可以使用表格而不是使用序列。
答案 3 :(得分:1)
我注意到您构建了一个像example这样的复合主键。当我在自己的数据库中查找类似的问题时,我想知道是否可以直接调用sql:
select nextval ('hibernate_sequence')
我认为那会欺骗; - )