冬眠ID增量神谕 [英] hibernate ID increment oracle

查看:281
本文介绍了冬眠ID增量神谕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法让Hibernate通过把这个到每个实体类递增1每个表的ID。

I managed to let hibernate increment the id of every table by 1 by putting this into every entity class.

@Entity
public class TestObject implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="idgen")
@SequenceGenerator(
        name="idgen",
        sequenceName="testobject_seq",
        allocationSize=1,
        initialValue=1
    )

我有两个表,我手工填写的数据通过Oracle SQL Developer中。之后,我创建的表。

I have two tables which i fill with data manually through the Oracle SQL Developer. After i created the tables.

<property name="hibernate.hbm2ddl.auto">create</property>

有关的两张桌子,在它的数据我设置为初值我需要什么。
例如,表testchamber有22行数据。所以我的注释变为这样:

For the 2 tables with data in it i set the initialValue to what i need. For instance, table testchamber has 22 rows with data. So my annotation changes to this:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="idgen")
@SequenceGenerator(
        name="idgen",
        sequenceName="testchamber_seq",
        allocationSize=1,
        initialValue=23
    )

但是,当我尝试保存新testChamber实体我得到这个错误。

But when i try to save a new testChamber entity i get this error.

org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session

我可以节省我的实体没有问题之前,我改变了注释,但与旧的注释休眠随机递增的ID。例如,休眠,得到一个新的实体的ID 60,61,而不是23,24等..

I could save my entity without a problem before i changed the annotation but with the old annotation hibernate incremented the id randomly. For instance, hibernate gave a new entity the id 60, 61 instead of 23, 24 and so on..

这是我的老注释:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)


  • 有没有一种正确的方式,告诉Hibernate一个表早已
    在它,它应该开始的数据来算,形成一个具体的数字?

  • 或我怎么能摆脱NonUniqueObjectException的。

  • 推荐答案

    这个职位让我与我的问题<一个href=\"http://stackoverflow.com/questions/14152348/generated-sequence-starts-with-1-instead-of-1000-which-is-set-in-annotation\">link\".
    但是从@nolexa的雁是正确的,而不是来自这实际上是检查为正确亚历克斯Gitelman。
    我把这个

    This post helped me with my problem "link". But the anser from @nolexa is correct, not from Alex Gitelman which is actually checked as correct. I put this

    <property name="hibernate.id.new_generator_mappings">true</property>
    

    在我的hibernate.cfg.xml文件和我所有的创建序列现在正常工作。非常感谢你!@nolexa

    into my hibernate.cfg.xml file and all my created sequences work fine now. Thank you very much @nolexa!!

    这篇关于冬眠ID增量神谕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆