首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JPA组合键+序列

JPA组合键+序列
EN

Stack Overflow用户
提问于 2008-10-28 11:08:57
回答 3查看 16.9K关注 0票数 17

在普通的JPA或JPA+Hibernate扩展中,是否可以声明组合键,其中组合键的元素是序列?

这是我的复合类:

代码语言:javascript
运行
复制
@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;
    }

    //...
}

我已经提供了applicationentitynativeIdnativeKey的值。我想构建一个如下所示的实体:

代码语言:javascript
运行
复制
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)。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-02-24 20:49:43

我相信这在普通的JPA中是不可能的。

票数 3
EN

Stack Overflow用户

发布于 2008-10-28 17:00:18

试着这样做:

代码语言:javascript
运行
复制
@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;
}

通过这种方式,您可以使用表而不是序列。

票数 1
EN

Stack Overflow用户

发布于 2009-03-30 15:00:29

我注意到您构建的复合主键看起来像这样的example。当我在自己的数据库中遇到类似的问题时,我想知道是否可以直接调用sql,如下所示:

select nextval ('hibernate_sequence')

我认为这是作弊;-)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/242914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档