【问题标题】:I need @Id @GeneratedValue(strategy = GenerationType.IDENTITY) on other column before insert在插入之前,我需要在其他列上使用 @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
【发布时间】:2015-01-11 19:13:08
【问题描述】:

我有一个带有主键的实体,例如下面的“id”。随着该实体的发展,我有另一列将围绕该组的第一个“id”对该实体进行分组。

简而言之,如果“idstore”为空,则第二列“idstore”应插入“id”值,否则插入“idstore”值。我怎样才能用 Hibernate 实现这一点?我正在使用休眠 4.0.1

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
@Column(nullable=flase)
Integer idstore;

这个想法是在 db 中有一个类似的情况(假设两个组的 id 为 1 和 3)

+----+---------+
| id | idstore | 
+----+---------+
|  1 |       1 |
+----+---------+
|  2 |       1 |
+----+---------+
|  3 |       3 |
+----+---------+
|  4 |       1 |
+----+---------+
|  5 |       3 |
+----+---------+

由于没有答案,我不知道如何使用 JPA 处理这个问题,所以我在数据库上添加了一个触发器来完成这项工作。不漂亮,但只有快速出路。

【问题讨论】:

    标签: hibernate postgresql jpa


    【解决方案1】:

    请查看this link

    相信@PrePersist 可能会在这种情况下帮助您。

    【讨论】:

    • 很遗憾没有,因为在@PrePersist@Id 尚未创建,所以“id”仍然为空。
    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 2012-02-15
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2011-03-05
    • 2021-11-28
    相关资源
    最近更新 更多