Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 在插入之前,我需要在其他列上显示@Id@GeneratedValue(strategy=GenerationType.IDENTITY)_Hibernate_Postgresql_Jpa - Fatal编程技术网

Hibernate 在插入之前,我需要在其他列上显示@Id@GeneratedValue(strategy=GenerationType.IDENTITY)

Hibernate 在插入之前,我需要在其他列上显示@Id@GeneratedValue(strategy=GenerationType.IDENTITY),hibernate,postgresql,jpa,Hibernate,Postgresql,Jpa,我有一个主键为“id”的实体。随着这个实体的发展,我有另一个专栏,它将围绕这个组的第一个“id”对这个实体进行分组 简而言之,如果“idstore”为空,则第二列“idstore”应插入“id”值,否则插入“idstore”值。我如何通过Hibernate实现这一点?我正在使用Hibernate4.0.1 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Integer id; @Column(nullable=flase) Inte

我有一个主键为“id”的实体。随着这个实体的发展,我有另一个专栏,它将围绕这个组的第一个“id”对这个实体进行分组

简而言之,如果“idstore”为空,则第二列“idstore”应插入“id”值,否则插入“idstore”值。我如何通过Hibernate实现这一点?我正在使用Hibernate4.0.1

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
@Column(nullable=flase)
Integer idstore;
想法是在db中有一个模拟的情况(假设两组id为1和3)

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

请看一看


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

遗憾的是,没有,因为在
@PrePersist
中,
@Id
尚未创建,所以“Id”仍然为空。
+----+---------+
| id | idstore | 
+----+---------+
|  1 |       1 |
+----+---------+
|  2 |       1 |
+----+---------+
|  3 |       3 |
+----+---------+
|  4 |       1 |
+----+---------+
|  5 |       3 |
+----+---------+