Hibernate生成类

发布于 2024-12-14 19:48:40 字数 627 浏览 1 评论 0原文

我正在使用 hibernate 应用程序将数据存储在 Postgres 数据库中,我已经从 POJO 创建了 HBM 文件。该表由 ID 组成,该 ID 与 bigserial 相同,它应该自动生成。但是当我在插入记录后运行项目时,它不会增加 id 的值,因此会引发异常:

 SEVERE: ERROR: duplicate key value violates unique constraint "usertable_pkey"
 SEVERE: Could not synchronize database state with session
 org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

我的 HBM 条目如下:

<id name="userid" type="long">
        <column name="userid" />
        <generator class="assigned" />
    </id>

谁能建议我可以使用哪个生成器类来解决此问题?

I am using a hibernate application to store data in Postgres database I have created HBM file from POJO. The table consist of ID which is bigserial identically it should be autogenerated. but when i run project after insertion of a record it doesnt increament value of id hence throws exception:

 SEVERE: ERROR: duplicate key value violates unique constraint "usertable_pkey"
 SEVERE: Could not synchronize database state with session
 org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

My HBM entry is as follows:

<id name="userid" type="long">
        <column name="userid" />
        <generator class="assigned" />
    </id>

Can anyone suggest which generator class I can use to resolve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2024-12-21 19:48:40

当您插入事务数据时,最好使用[sequence][2] 来生成对象的 id。

<id ....>
        <generator class="sequence">
           <param name="sequence">YOUR_SEQUENCE _NAME</param>
       </generator>
</id>

这将确保即使在多线程环境中,您也会为不同的对象获得不同的 id。

此处阅读有关生成器类的 API

When you are inserting transactional data it is prefered to use a[sequence][2] to generate the id if the object.

<id ....>
        <generator class="sequence">
           <param name="sequence">YOUR_SEQUENCE _NAME</param>
       </generator>
</id>

this will ensure that even in multi-threaded environments you will get different id for different objects.

Read the API about generator classes here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文