在普通的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)。
发布于 2009-02-24 20:49:43
我相信这在普通的JPA中是不可能的。
发布于 2008-10-28 17:00:18
试着这样做:
@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;
}通过这种方式,您可以使用表而不是序列。
发布于 2009-03-30 15:00:29
我注意到您构建的复合主键看起来像这样的example。当我在自己的数据库中遇到类似的问题时,我想知道是否可以直接调用sql,如下所示:
select nextval ('hibernate_sequence')
我认为这是作弊;-)
https://stackoverflow.com/questions/242914
复制相似问题