小编BOS*_*dis的帖子

如何在@Embeddable中使用@GenerateValue用作复合主键?

我的数据库中有一张带有主组合键的表,当我使用 Hibernate 对其进行建模时,我使用@EmbeddedId& @Embedable。该复合主键的一列具有带有 的生成值@GeneratedValue(strategy = GenerationType.IDENTITY)

当我尝试在数据库中创建两个新对象时,出现如下错误:

org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [MyPackage#MyEmbedableClass [MyGeneratedPrimaryKeyValue=null, OtherPrimaryKey=21]]
Run Code Online (Sandbox Code Playgroud)

但是当我查看数据库时,我的对象已创建。我不明白我的错误,我不知道如何解决我的问题。

我找到了一些像我这样的主题,但还没有找到我的问题的答案。

  1. 第一个例子

  2. 第二个例子

  3. 第三个例子

org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [MyPackage#MyEmbedableClass [MyGeneratedPrimaryKeyValue=null, OtherPrimaryKey=21]]
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助。

hibernate composite primary-key auto-generate

3
推荐指数
1
解决办法
2640
查看次数

标签 统计

auto-generate ×1

composite ×1

hibernate ×1

primary-key ×1